Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typescript-cp

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-cp - npm Package Compare versions

Comparing version 0.1.0 to 0.1.2

15

CHANGELOG.md

@@ -5,2 +5,17 @@ # Changelog

### [0.1.2](https://github.com/body-builder/typescript-cp/compare/v0.1.1...v0.1.2) (2021-06-14)
### Bug Fixes
* Version bump after accidentally published v0.1.1 ([fa9b4fd](https://github.com/body-builder/typescript-cp/commit/fa9b4fdc4860ebc4c9f311272487be7311249e0f))
### [0.1.1](https://github.com/body-builder/typescript-cp/compare/v0.1.0...v0.1.1) (2021-06-14)
### Features
* Advanced loader rule conditions ([311c9ae](https://github.com/body-builder/typescript-cp/commit/311c9ae761288ee12706b11978e584cc4960c23c))
* Basic loader functionality ([2b90d96](https://github.com/body-builder/typescript-cp/commit/2b90d964a29c1282dd2f333bbbda89975bbe06f2))
### 0.1.0 (2021-03-29)

@@ -7,0 +22,0 @@

28

dist/actions.d.ts

@@ -1,10 +0,10 @@

import { TsProjectWithFiles, TsProject } from './types';
import { Config, TsProjectWithFiles, TsProject } from './types';
/**
* Returns an extended TsProject array, containing also the paths of all files in the project that will be processed
* @param projects
* @param ignored_files
* @param config
*/
declare function collect_projects_files(projects: TsProject[], { ignored_files }: {
ignored_files: any;
}): Promise<TsProjectWithFiles[]>;
declare function collect_projects_files(projects: TsProject[], config: Config): Promise<TsProjectWithFiles[]>;
/**
* Returns a flat array with the source and target paths of all files that will be processed
* @param globed_projects

@@ -17,19 +17,13 @@ */

/**
* Copies all files from the source path to the output path
* @param projects
* @param cwd
* @param ignored_files
* @param config
*/
declare function copy_files(projects: TsProject[], { cwd, ignored_files }: {
cwd: any;
ignored_files: any;
}): Promise<void>;
declare function copy_files(projects: TsProject[], config: Config): Promise<void>;
/**
* Starts a watcher listening to the changes in the source path and synchronizing them to the output path
* @param projects
* @param cwd
* @param ignored_files
* @param config
*/
declare function watch_files(projects: TsProject[], { cwd, ignored_files }: {
cwd: any;
ignored_files: any;
}): Promise<void>;
declare function watch_files(projects: TsProject[], config: Config): Promise<void>;
export { collect_projects_files, collect_projects_files_flat, copy_files, watch_files, };

@@ -78,10 +78,10 @@ "use strict";

/**
* Returns an extended TsProject array, containing also the paths of all files in the project that will be processed
* @param projects
* @param ignored_files
* @param config
*/
function collect_projects_files(projects, _a) {
var ignored_files = _a.ignored_files;
function collect_projects_files(projects, config) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_b) {
return __generator(this, function (_a) {
// console.log('collect_projects_files');

@@ -99,3 +99,3 @@ if (!Array.isArray(projects)) {

onlyFiles: true,
ignore: ignored_files,
ignore: config.ignored_files,
})];

@@ -113,2 +113,3 @@ case 1:

/**
* Returns a flat array with the source and target paths of all files that will be processed
* @param globed_projects

@@ -150,19 +151,18 @@ */

/**
* Copies all files from the source path to the output path
* @param projects
* @param cwd
* @param ignored_files
* @param config
*/
function copy_files(projects, _a) {
var cwd = _a.cwd, ignored_files = _a.ignored_files;
function copy_files(projects, config) {
return __awaiter(this, void 0, void 0, function () {
var globed_projects, all_projects_files;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, collect_projects_files(projects, { ignored_files: ignored_files })];
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, collect_projects_files(projects, config)];
case 1:
globed_projects = _b.sent();
globed_projects = _a.sent();
return [4 /*yield*/, collect_projects_files_flat(globed_projects)];
case 2:
all_projects_files = _b.sent();
all_projects_files = _a.sent();
// Delete files

@@ -181,3 +181,3 @@ // we should do this in two steps, to avoid possible deletion of already linked files (when a folder gets )

// we should do this in two steps, to avoid possible deletion of already linked files (when a folder gets )
_b.sent();
_a.sent();
// Link files

@@ -188,3 +188,3 @@ return [4 /*yield*/, Promise.all(all_projects_files.map(function (_a) {

return __generator(this, function (_b) {
return [2 /*return*/, helpers_1.copy_file_or_directory(source_path, target_path)];
return [2 /*return*/, helpers_1.copy_file_or_directory(source_path, target_path, config)];
});

@@ -195,3 +195,3 @@ });

// Link files
_b.sent();
_a.sent();
return [2 /*return*/];

@@ -204,11 +204,10 @@ }

/**
* Starts a watcher listening to the changes in the source path and synchronizing them to the output path
* @param projects
* @param cwd
* @param ignored_files
* @param config
*/
function watch_files(projects, _a) {
var cwd = _a.cwd, ignored_files = _a.ignored_files;
function watch_files(projects, config) {
return __awaiter(this, void 0, void 0, function () {
/**
* Find the TS project to which the requested file belongs to
* Find the TS project the requested file belongs to
* @param source_path

@@ -271,7 +270,7 @@ */

var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, collect_projects_files(projects, { ignored_files: ignored_files })];
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, collect_projects_files(projects, config)];
case 1:
globed_projects = _b.sent();
globed_projects = _a.sent();
files_to_watch = globed_projects.map(function (_a) {

@@ -281,3 +280,3 @@ var root_dir = _a.root_dir;

});
ignore_glob = "{" + ignored_files.map(function (rule) { return "**/" + rule; }).join(',') + "}";
ignore_glob = "{" + config.ignored_files.map(function (rule) { return "**/" + rule; }).join(',') + "}";
watcher = chokidar_1.default.watch(files_to_watch, {

@@ -305,3 +304,3 @@ ignored: ignore_glob,

_a = get_target_path(source_path), target_path = _a.target_path, project_name = _a.project_name, filename = _a.filename;
return [4 /*yield*/, helpers_1.copy_file_or_directory(source_path, target_path)];
return [4 /*yield*/, helpers_1.copy_file_or_directory(source_path, target_path, config)];
case 1:

@@ -325,3 +324,3 @@ _b.sent();

_a = get_target_path(source_path), target_path = _a.target_path, project_name = _a.project_name, filename = _a.filename;
return [4 /*yield*/, helpers_1.copy_file_or_directory(source_path, target_path)];
return [4 /*yield*/, helpers_1.copy_file_or_directory(source_path, target_path, config)];
case 1:

@@ -328,0 +327,0 @@ _b.sent();

@@ -44,3 +44,3 @@ #!/usr/bin/env node

return __awaiter(this, void 0, void 0, function () {
var config, cli_options, cwd, ignored_files, projects;
var config, cli_options, projects;
return __generator(this, function (_a) {

@@ -51,6 +51,6 @@ switch (_a.label) {

config = _a.sent();
cli_options = config.cli_options, cwd = config.cwd, ignored_files = config.ignored_files;
cli_options = config.cli_options;
projects = cli_options.build ? helpers_1.get_ts_projects_paths(config) : [helpers_1.get_ts_project_paths(config)];
if (cli_options.watch) {
return [2 /*return*/, actions_1.watch_files(projects, { cwd: cwd, ignored_files: ignored_files })
return [2 /*return*/, actions_1.watch_files(projects, config)
.then(function () {

@@ -68,3 +68,3 @@ if (projects) {

}
return [2 /*return*/, actions_1.copy_files(projects, { cwd: cwd, ignored_files: ignored_files })
return [2 /*return*/, actions_1.copy_files(projects, config)
.then(function () {

@@ -71,0 +71,0 @@ if (projects) {

/// <reference types="node" />
import * as fs from 'fs';
import ts from 'typescript';
import { ParsedCommandLine } from 'typescript';
import { Config, TsProject } from './types';

@@ -51,12 +51,33 @@ declare const promisified: {

};
/**
* Returns the complete, resolved configuration object
*/
declare function get_config(): Promise<Config>;
declare function get_ts_config(currentDir: string, project: string): ts.ParsedCommandLine;
/**
* Finds the `project` config file in `currentDir` and parses it with Typescript's own parser
* @param currentDir
* @param project
*/
declare function get_ts_config(currentDir: string, project: string): ParsedCommandLine;
/**
* Returns the internal project descriptor of a TS project that doesn't have project references
* @param options
*/
declare function get_ts_project_paths(options: Config): TsProject;
/**
* Returns the internal project descriptor of a TS project that has project references
* @param options
*/
declare function get_ts_projects_paths(options: Config): TsProject[];
/**
* Displays a colored log in the stdout
* @param msg
* @param color
*/
declare function color_log(msg: string, color: typeof console_colors[keyof typeof console_colors]): string;
/**
* Makes sure that the given folder exists - creates if not
* @param path {string}
* Makes sure that the given folder (or the parent folder of a file) exists - creates if not
* @param p {string}
*/
declare function validate_path(path: string): Promise<void>;
declare function validate_path(p: string): Promise<void>;
/**

@@ -68,8 +89,18 @@ * Error-safe `fs.lstat` - returns stats if the file exists, otherwise null

/**
*
* Deletes the `file_path` file. Doesn't throw error if the file doesn't exist.
* @param file_path
*/
declare function remove_file_or_directory(file_path: string): Promise<void>;
declare function copy_file_or_directory(source_path: string, destination_path: string): Promise<any>;
/**
* Reads the content of the `source_path` file, applies the loaders on its content, and writes the processed content to `destination_path`
* @param source_path
* @param destination_path
* @param config
*/
declare function copy_file_or_directory(source_path: string, destination_path: string, config: Config): Promise<any>;
/**
* Returns a Promise that resolves automatically after `ms`
* @param ms
*/
declare function sleep(ms: any): Promise<unknown>;
export { get_config, get_ts_config, get_ts_project_paths, get_ts_projects_paths, validate_path, get_file_stats, remove_file_or_directory, copy_file_or_directory, sleep, promisified, console_colors, color_log, };

@@ -103,3 +103,3 @@ "use strict";

var promisified = {
fs: __assign(__assign({}, pify_1.default(fs)), { exists: pify_1.default(fs.exists, { errorFirst: false }) }),
fs: __assign(__assign({}, pify_1.default(fs)), { exists: pify_1.default(fs.exists, { errorFirst: false }), mkdir: pify_1.default(fs.mkdir, { errorFirst: false }) }),
fse: pify_1.default(fs_extra_1.default),

@@ -171,2 +171,5 @@ rimraf: pify_1.default(rimraf_1.default),

var options = program.opts();
/**
* Returns the complete, resolved configuration object
*/
function get_config() {

@@ -190,2 +193,3 @@ return __awaiter(this, void 0, void 0, function () {

ignored_files: ['node_modules'],
rules: [],
ts_config: ts_config,

@@ -201,2 +205,7 @@ };

exports.get_config = get_config;
/**
* Finds the `project` config file in `currentDir` and parses it with Typescript's own parser
* @param currentDir
* @param project
*/
function get_ts_config(currentDir, project) {

@@ -210,2 +219,8 @@ var configFile = typescript_1.default.findConfigFile(currentDir, typescript_1.default.sys.fileExists, project);

exports.get_ts_config = get_ts_config;
/**
* Creates an internal project descriptor for one Typescript project
* @param cwd
* @param project_path
* @param ts_config
*/
function build_project_path(cwd, project_path, ts_config) {

@@ -230,2 +245,6 @@ var currentDirName = path.basename(path.resolve());

}
/**
* Returns the internal project descriptor of a TS project that doesn't have project references
* @param options
*/
function get_ts_project_paths(options) {

@@ -236,2 +255,6 @@ var cwd = options.cwd, cli_options = options.cli_options, ts_config = options.ts_config;

exports.get_ts_project_paths = get_ts_project_paths;
/**
* Returns the internal project descriptor of a TS project that has project references
* @param options
*/
function get_ts_projects_paths(options) {

@@ -255,2 +278,7 @@ var ts_config = options.ts_config;

exports.get_ts_projects_paths = get_ts_projects_paths;
/**
* Displays a colored log in the stdout
* @param msg
* @param color
*/
function color_log(msg, color) {

@@ -261,13 +289,16 @@ return "" + color + msg + console_colors.Reset;

/**
* Makes sure that the given folder exists - creates if not
* @param path {string}
* Makes sure that the given folder (or the parent folder of a file) exists - creates if not
* @param p {string}
*/
function validate_path(path) {
function validate_path(p) {
return __awaiter(this, void 0, void 0, function () {
var dirname;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, promisified.fs.exists(path)];
case 0:
dirname = path.dirname(p);
return [4 /*yield*/, promisified.fs.exists(dirname)];
case 1:
if (!!(_a.sent())) return [3 /*break*/, 3];
return [4 /*yield*/, promisified.fs.mkdir(path, { recursive: true })];
return [4 /*yield*/, promisified.fs.mkdir(dirname, { recursive: true })];
case 2:

@@ -305,3 +336,3 @@ _a.sent();

/**
*
* Deletes the `file_path` file. Doesn't throw error if the file doesn't exist.
* @param file_path

@@ -327,5 +358,12 @@ */

exports.remove_file_or_directory = remove_file_or_directory;
function copy_file_or_directory(source_path, destination_path) {
var files_without_loaders = [];
/**
* Reads the content of the `source_path` file, applies the loaders on its content, and writes the processed content to `destination_path`
* @param source_path
* @param destination_path
* @param config
*/
function copy_file_or_directory(source_path, destination_path, config) {
return __awaiter(this, void 0, void 0, function () {
var stats, is_directory;
var stats, is_directory, raw_content, processed_content;
return __generator(this, function (_a) {

@@ -343,4 +381,12 @@ switch (_a.label) {

}
// console.log('COPY', link_target, 'to', link_path);
return [2 /*return*/, fs_extra_1.default.copy(source_path, destination_path)];
return [4 /*yield*/, promisified.fse.readFile(source_path, 'utf8')];
case 2:
raw_content = _a.sent();
return [4 /*yield*/, apply_loaders(raw_content, source_path, destination_path, config)];
case 3:
processed_content = _a.sent();
return [4 /*yield*/, validate_path(destination_path)];
case 4:
_a.sent();
return [2 /*return*/, promisified.fse.writeFile(destination_path, processed_content, 'utf8')];
}

@@ -351,2 +397,99 @@ });

exports.copy_file_or_directory = copy_file_or_directory;
/**
* Tests the given `source_path` against a given rule condition
* @param source_path
* @param rule_condition
* @param config
*/
function test_rule_condition(source_path, rule_condition, config) {
if (Array.isArray(rule_condition)) {
return rule_condition.every(function (sub_condition) { return test_rule_condition(source_path, sub_condition, config); });
}
// An exact absolute path string
if (typeof rule_condition === 'string') {
return rule_condition === source_path;
}
if (rule_condition instanceof RegExp) {
return rule_condition.test(source_path);
}
if (typeof rule_condition === 'function') {
return rule_condition(source_path);
}
return false;
}
/**
* Tests the given `source_path` against every rule conditions
* @param source_path
* @param rule
* @param config
*/
function apply_rule_condition(source_path, rule, config) {
var isMatching = rule.test !== undefined ? test_rule_condition(source_path, rule.test, config) : null;
var isIncluded = rule.include !== undefined ? test_rule_condition(source_path, rule.include, config) : null;
var isExcluded = rule.exclude !== undefined ? test_rule_condition(source_path, rule.exclude, config) : null;
if (isIncluded === true) {
return true;
}
if (isExcluded === true) {
return false;
}
if (isMatching === true) {
return true;
}
return false;
}
/**
* Passes the content of the source file to each loader in sequence and returns a Promise with the final content
* @param raw_content
* @param source_path
* @param destination_path
* @param config
*/
function apply_loaders(raw_content, source_path, destination_path, config) {
return __awaiter(this, void 0, void 0, function () {
var processed_content, should_process_file, used_rules, loaderMeta_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
processed_content = raw_content;
should_process_file = files_without_loaders.indexOf(source_path) === -1;
if (!should_process_file) return [3 /*break*/, 2];
used_rules = config.rules.filter(function (rule) { return apply_rule_condition(source_path, rule, config); });
if (!used_rules.length) {
files_without_loaders.push(source_path);
return [2 /*return*/, raw_content];
}
loaderMeta_1 = {
source_path: source_path,
destination_path: destination_path,
config: config,
};
return [4 /*yield*/, used_rules.reduce(function (content, rule) {
return __spreadArray([], __read(rule.use)).reverse().reduce(function (content, loader) {
var loaderFn;
switch (typeof loader.loader) {
case 'function':
loaderFn = loader.loader;
break;
case 'string':
loaderFn = require(path.resolve(loader.loader));
break;
default:
throw new Error('Invalid loader type');
}
return loaderFn(content, loaderMeta_1);
}, processed_content);
}, processed_content)];
case 1:
processed_content = _a.sent();
_a.label = 2;
case 2: return [2 /*return*/, processed_content];
}
});
});
}
/**
* Returns a Promise that resolves automatically after `ms`
* @param ms
*/
function sleep(ms) {

@@ -353,0 +496,0 @@ return new Promise(function (resolve) { return setTimeout(resolve, ms); });

import { ParsedCommandLine } from 'typescript';
export interface CliOptions {
export declare type CliOptions = {
watch?: boolean;
build?: boolean;
project: string;
}
export interface Config {
};
export declare type Config = {
cwd: string;
cli_options: CliOptions;
ts_config: ParsedCommandLine;
compiled_files: [];
ignored_files: [];
}
export interface TsProject {
compiled_files: string[];
ignored_files: string[];
rules: Rule[];
};
export declare type Rule = {
test?: RuleCondition;
include?: RuleCondition;
exclude?: RuleCondition;
use: Loader[];
options: {
[key: string]: any;
};
};
/**
* Resource paths always meant to be absolute paths.
*/
export declare type RuleCondition = RegExp | string | ((path: string) => boolean) | RuleConditions;
export declare type RuleConditions = RuleCondition[];
export declare type Loader = {
/**
* A path reference to the loader function, or the loader function itself.
*/
loader: string | LoaderFunction;
};
/**
* The loader function accepts the actual content of the given file as the first parameter, and must return the content of the output file.
*/
export declare type LoaderFunction = (content: string, meta: LoaderMeta) => string;
export declare type LoaderMeta = {
source_path: string;
destination_path: string;
config: Config;
};
export declare type TsProject = {
project_name: string;

@@ -20,5 +50,5 @@ base_path: string;

out_dir: string;
}
export interface TsProjectWithFiles extends TsProject {
};
export declare type TsProjectWithFiles = TsProject & {
source_files: string[];
}
};
{
"name": "typescript-cp",
"version": "0.1.0",
"version": "0.1.2",
"description": "Copy non-typescript files to outDir",

@@ -37,3 +37,3 @@ "homepage": "https://github.com/body-builder/typescript-cp#readme",

"cosmiconfig": "^7.0.0",
"fs-extra": "^9.1.0",
"fs-extra": "^10.0.0",
"globby": "^11.0.3",

@@ -49,3 +49,3 @@ "lodash": "^4.17.21",

"devDependencies": {
"@types/node": "^14.14.35",
"@types/node": "^15.12.2",
"@types/rimraf": "^3.0.0",

@@ -52,0 +52,0 @@ "typescript": "^4.2.3"

@@ -59,3 +59,40 @@ # Typescript-cp

# Loaders
You can attach basic loader rules to the files. Loaders accept the actual content of the given file as the first parameter, and must return the content of the output file.
.tscprc.js
```js
const path = require('path');
/**
* @type {import('typescript-cp/dist/types').Config}
*/
module.exports = {
rules: [
{
test: /\.(scss|sass)$/,
include: [
path.resolve('./file-to-include.css'),
],
exclude: (source_path) => {
return source_path.indexOf('file-to-include.sass') > -1;
},
use: [
{
loader: (content, meta) => {
// Do something with `content`
return content;
},
},
],
},
],
};
```
See `Config` and `Rule` types in `src/types.ts` for the complete reference.
## Contribution

@@ -62,0 +99,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc