typescript-cp
Version:
Copy non-typescript files to outDir
386 lines (385 loc) • 20.6 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.watch_files = exports.copy_files = exports.collect_projects_files_flat = exports.collect_projects_files = void 0;
var path = __importStar(require("path"));
var globby_1 = __importDefault(require("globby"));
var chokidar_1 = __importDefault(require("chokidar"));
var helpers_1 = require("./helpers");
/**
* Returns an extended TsProject array, containing also the paths of all files in the project that will be processed
* @param projects
* @param config
*/
function collect_projects_files(projects, config) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
// console.log('collect_projects_files');
if (!Array.isArray(projects)) {
throw new Error('No project data received');
}
return [2 /*return*/, Promise.all(projects.map(function (project) { return __awaiter(_this, void 0, void 0, function () {
var source_files;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, globby_1.default)('**/*', {
cwd: project.root_dir,
dot: true,
onlyFiles: true,
ignore: (0, helpers_1.get_ignore_list)(config, project),
})];
case 1:
source_files = _a.sent();
return [2 /*return*/, __assign(__assign({}, project), { source_files: source_files })];
}
});
}); }))];
});
});
}
exports.collect_projects_files = collect_projects_files;
/**
* Returns a flat array with the source and target paths of all files that will be processed
* @param globed_projects
*/
function collect_projects_files_flat(globed_projects) {
return __awaiter(this, void 0, void 0, function () {
var all_projects_files;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all(globed_projects.map(function (project) { return __awaiter(_this, void 0, void 0, function () {
var root_dir, out_dir, source_files;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
root_dir = project.root_dir, out_dir = project.out_dir, source_files = project.source_files;
return [4 /*yield*/, Promise.all(source_files.map(function (file) { return __awaiter(_this, void 0, void 0, function () {
var source_path, target_path;
return __generator(this, function (_a) {
source_path = path.resolve(root_dir, file);
target_path = path.resolve(out_dir, file);
return [2 /*return*/, { source_path: source_path, target_path: target_path }];
});
}); }))];
case 1: return [2 /*return*/, _a.sent()];
}
});
}); }))];
case 1:
all_projects_files = _a.sent();
return [2 /*return*/, all_projects_files.flat()];
}
});
});
}
exports.collect_projects_files_flat = collect_projects_files_flat;
/**
* Copies all files from the source path to the output path
* @param projects
* @param config
*/
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 (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, collect_projects_files(projects, config)];
case 1:
globed_projects = _a.sent();
return [4 /*yield*/, collect_projects_files_flat(globed_projects)];
case 2:
all_projects_files = _a.sent();
// Delete files
// we should do this in two steps, to avoid possible deletion of already linked files (when a folder gets )
return [4 /*yield*/, Promise.all(all_projects_files.map(function (_a) {
var source_path = _a.source_path, target_path = _a.target_path;
return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_b) {
return [2 /*return*/, (0, helpers_1.remove_file_or_directory)(target_path)];
});
});
}))];
case 3:
// Delete files
// we should do this in two steps, to avoid possible deletion of already linked files (when a folder gets )
_a.sent();
// Link files
return [4 /*yield*/, Promise.all(all_projects_files.map(function (_a) {
var source_path = _a.source_path, target_path = _a.target_path;
return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_b) {
return [2 /*return*/, (0, helpers_1.copy_file_or_directory)(source_path, target_path, config)];
});
});
}))];
case 4:
// Link files
_a.sent();
return [2 /*return*/];
}
});
});
}
exports.copy_files = copy_files;
/**
* Starts a watcher listening to the changes in the source path and synchronizing them to the output path
* @param projects
* @param config
*/
function watch_files(projects, config) {
return __awaiter(this, void 0, void 0, function () {
/**
* Find the TS project the requested file belongs to
* @param source_path
*/
function get_target_path(source_path) {
var parent_project = globed_projects.find(function (_a) {
var base_path = _a.base_path;
return source_path.startsWith(base_path);
});
if (!parent_project) {
throw new Error("Could not find the TS project to which the file belongs to: '".concat(source_path, "'"));
}
var filename = path.relative(parent_project.root_dir, source_path);
var target_path = path.resolve(parent_project.out_dir, filename);
return {
project_name: parent_project.project_name,
source_path: source_path,
filename: filename,
target_path: target_path,
};
}
function watch_idle_log(msg, timeout, clear) {
if (msg === void 0) { msg = 'Watching files for changes'; }
if (timeout === void 0) { timeout = 1000; }
if (clear === void 0) { clear = true; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, helpers_1.sleep)(1000)];
case 1:
_a.sent();
if (clear) {
console.clear();
}
console.log(msg);
console.log((0, helpers_1.color_log)(globed_projects.map(function (_a) {
var project_name = _a.project_name;
return "".concat(project_name);
}).join('\n'), helpers_1.console_colors.FgBrightBlack));
return [2 /*return*/];
}
});
});
}
function close_watcher() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, watcher.close()];
case 1:
_a.sent();
console.log('\nFile watcher gracefully shut down.');
return [2 /*return*/];
}
});
});
}
var globed_projects, files_to_watch, ignore_glob, watcher, isReady;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, collect_projects_files(projects, config)];
case 1:
globed_projects = _a.sent();
files_to_watch = globed_projects.map(function (_a) {
var root_dir = _a.root_dir;
return "".concat(root_dir, "/.");
});
ignore_glob = "{".concat((0, helpers_1.get_ignore_list)(config, projects).map(function (rule) {
if (!rule.startsWith('**/')) {
return "**/".concat(rule);
}
return rule;
}).join(','), "}");
watcher = chokidar_1.default.watch(files_to_watch, {
ignored: ignore_glob,
});
isReady = false;
watcher
.on('ready', function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, watch_idle_log()];
case 1:
_a.sent();
isReady = true;
return [2 /*return*/];
}
});
}); })
.on('add', function (unsafe_source_path) { return __awaiter(_this, void 0, void 0, function () {
var source_path, _a, target_path, project_name, filename;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
source_path = (0, helpers_1.definitely_posix)(unsafe_source_path);
_a = get_target_path(source_path), target_path = _a.target_path, project_name = _a.project_name, filename = _a.filename;
return [4 /*yield*/, (0, helpers_1.copy_file_or_directory)(unsafe_source_path, target_path, config)];
case 1:
_b.sent();
if (!isReady) return [3 /*break*/, 3];
console.log((0, helpers_1.color_log)("".concat(project_name, "/").concat(filename), helpers_1.console_colors.FgGreen), 'added');
return [4 /*yield*/, watch_idle_log()];
case 2:
_b.sent();
_b.label = 3;
case 3: return [2 /*return*/];
}
});
}); })
.on('change', function (unsafe_source_path) { return __awaiter(_this, void 0, void 0, function () {
var source_path, _a, target_path, project_name, filename;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
source_path = (0, helpers_1.definitely_posix)(unsafe_source_path);
_a = get_target_path(source_path), target_path = _a.target_path, project_name = _a.project_name, filename = _a.filename;
return [4 /*yield*/, (0, helpers_1.copy_file_or_directory)(source_path, target_path, config)];
case 1:
_b.sent();
console.log((0, helpers_1.color_log)("".concat(project_name, "/").concat(filename), helpers_1.console_colors.FgYellow), 'changed');
return [4 /*yield*/, watch_idle_log()];
case 2:
_b.sent();
return [2 /*return*/];
}
});
}); })
.on('unlink', function (unsafe_source_path) { return __awaiter(_this, void 0, void 0, function () {
var source_path, _a, target_path, project_name, filename;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
source_path = (0, helpers_1.definitely_posix)(unsafe_source_path);
_a = get_target_path(source_path), target_path = _a.target_path, project_name = _a.project_name, filename = _a.filename;
return [4 /*yield*/, (0, helpers_1.remove_file_or_directory)(target_path)];
case 1:
_b.sent();
console.log((0, helpers_1.color_log)("".concat(project_name, "/").concat(filename), helpers_1.console_colors.FgRed), 'deleted');
return [4 /*yield*/, watch_idle_log()];
case 2:
_b.sent();
return [2 /*return*/];
}
});
}); });
process
.on('SIGINT', function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// console.log('SIGINT, closing watcher');
return [4 /*yield*/, close_watcher()];
case 1:
// console.log('SIGINT, closing watcher');
_a.sent();
return [2 /*return*/];
}
});
}); })
.on('SIGTERM', function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// console.log('SIGTERM, closing watcher');
return [4 /*yield*/, close_watcher()];
case 1:
// console.log('SIGTERM, closing watcher');
_a.sent();
return [2 /*return*/];
}
});
}); });
return [2 /*return*/];
}
});
});
}
exports.watch_files = watch_files;