UNPKG

2.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const migrate_1 = require("@kosko/migrate");
5const fs_1 = tslib_1.__importDefault(require("fs"));
6const get_stdin_1 = tslib_1.__importDefault(require("get-stdin"));
7const path_1 = require("path");
8const util_1 = require("util");
9const debug_1 = tslib_1.__importDefault(require("../cli/debug"));
10const print_1 = require("../cli/print");
11const debug = debug_1.default.extend("migrate");
12const stat = util_1.promisify(fs_1.default.stat);
13const readFile = util_1.promisify(fs_1.default.readFile);
14const readDir = util_1.promisify(fs_1.default.readdir);
15function concatFiles(arr) {
16 if (!arr.length)
17 return "";
18 let output = "";
19 for (const s of arr) {
20 if (!s.startsWith("---"))
21 output += "---\n";
22 output += s + "\n";
23 }
24 return output;
25}
26function readFileString(path) {
27 debug("Reading file", path);
28 return readFile(path, "utf8");
29}
30function readFilesInDir(dir) {
31 return tslib_1.__awaiter(this, void 0, void 0, function* () {
32 debug("Reading directory", dir);
33 const files = yield readDir(dir);
34 const contents = yield Promise.all(files.map(file => readFileString(path_1.join(dir, file))));
35 return concatFiles(contents);
36 });
37}
38function readFiles(cwd, files) {
39 return Promise.all(files.map((file) => tslib_1.__awaiter(this, void 0, void 0, function* () {
40 if (file === "-") {
41 debug("Reading from stdin");
42 return get_stdin_1.default();
43 }
44 const path = path_1.resolve(cwd, file);
45 const stats = yield stat(path);
46 return stats.isDirectory() ? readFilesInDir(path) : readFileString(path);
47 })));
48}
49function toArray(input) {
50 return Array.isArray(input) ? input : [input];
51}
52exports.migrateCmd = {
53 command: "migrate",
54 describe: "Migrate YAML into components",
55 builder(argv) {
56 /* istanbul ignore next */
57 return (argv
58 // HACK: Don't set the type of filename option to "array" because yargs
59 // can't parse `migrate -f -` properly.
60 // Link: https://github.com/tommy351/kosko/issues/17
61 .option("filename", {
62 type: "string",
63 describe: "File, directory to migrate",
64 required: true,
65 alias: "f"
66 })
67 .example("$0 migrate -f path/to/file", "Read from the path")
68 .example("$0 migrate -f -", "Read from stdin"));
69 },
70 handler(args) {
71 return tslib_1.__awaiter(this, void 0, void 0, function* () {
72 const file = concatFiles(yield readFiles(args.cwd, toArray(args.filename)));
73 const content = migrate_1.migrateString(file);
74 yield print_1.print(content);
75 });
76 }
77};
78//# sourceMappingURL=migrate.js.map
\No newline at end of file