|
8 | 8 | * @description |
9 | 9 | * |
10 | 10 | * ```text |
11 | | - * xt-sync [--gitignore] [--eslint] [--gitlab] [--travis] [--all] |
| 11 | + * xt-sync |
12 | 12 | * ``` |
13 | 13 | * |
14 | 14 | * The purpose of this command is to upgrade configuration files of |
|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | const path = require('path'); |
| 22 | +const prompts = require('prompts'); |
22 | 23 | const program = require('commander'); |
23 | 24 | const pkg = require('../package.json'); |
24 | 25 | const texts = require('../config/texts').xtSync; |
25 | 26 | const Utilities = require('./utilities').Utilities; |
26 | 27 |
|
| 28 | +// list available options |
27 | 29 | const files = { |
28 | | - gitlab: {path: '../config/gitlab.yml', out: '.gitlab-ci.yml'}, |
29 | | - travis: {path: '../config/travis.yml', out: '.travis.yml'}, |
30 | | - eslint: {path: '../config/eslint.json', out: '.eslintrc'}, |
31 | | - gitignore: {path: '../config/ignore', out: '.gitignore'} |
| 30 | + gitlab: {title: texts.argLint, path: '../config/gitlab.yml', out: '.gitlab-ci.yml'}, |
| 31 | + travis: {title: texts.argGitlab, path: '../config/travis.yml', out: '.travis.yml'}, |
| 32 | + eslint: {title: texts.argTravis, path: '../config/eslint.json', out: '.eslintrc'}, |
| 33 | + gitignore: {title: texts.gitignore, path: '../config/ignore', out: '.gitignore'} |
32 | 34 | }; |
33 | 35 |
|
| 36 | +// generate the options to display to user |
| 37 | +const options = [{ |
| 38 | + type: 'multiselect', |
| 39 | + name: 'options', |
| 40 | + message: texts.instructions, |
| 41 | + choices: Object.entries(files).map( |
| 42 | + ([key, value]) => ( |
| 43 | + {title: value.title, value: key} |
| 44 | + )) |
| 45 | +}]; |
| 46 | + |
34 | 47 | program |
| 48 | + .name('xt-sync') |
| 49 | + .option('-a --all', 'deprecated: call xt-sync without flags') |
35 | 50 | .version(pkg.version) |
36 | | - .option('-l --gitlab', texts.argGitlab) |
37 | | - .option('-t --travis', texts.argTravis) |
38 | | - .option('-e --eslint', texts.argLint) |
39 | | - .option('-g --gitignore', texts.argGitIgnore) |
40 | | - .option('-a --all', texts.argAll) |
41 | 51 | .parse(process.argv); |
42 | 52 |
|
43 | | -let counter = 0; |
| 53 | +(async () => { |
44 | 54 |
|
45 | | -const options = program.opts(); |
| 55 | + const onCancel = () => process.exit(0) |
| 56 | + // noinspection JSUnresolvedVariable |
| 57 | + const response = (await prompts(options, {onCancel})).options; |
46 | 58 |
|
47 | | -Object.keys(files).map(opt => { |
48 | | - if (options[opt] !== undefined || options.all) { |
49 | | - const relativePath = path.resolve(__dirname, files[opt].path); |
50 | | - const outputFileName = files[opt].out; |
51 | | - const content = Utilities.readFile(relativePath, 'utf8'); |
52 | | - const outPath = path.join(process.cwd(), outputFileName); |
| 59 | + // copy selected options from config -> project |
| 60 | + Object.entries(files).map(([key, value]) => { |
53 | 61 |
|
54 | | - Utilities.writeFile(outPath, content); |
55 | | - console.log(texts.updateSuccess(outputFileName)); |
56 | | - counter++; |
57 | | - } |
58 | | -}); |
| 62 | + if (response.indexOf(key) > -1) { |
| 63 | + const relativePath = path.resolve(__dirname, value.path); |
| 64 | + const content = Utilities.readFile(relativePath); |
| 65 | + const outPath = path.join(process.cwd(), value.out); |
59 | 66 |
|
60 | | -if (!counter) console.log(texts.onError); |
| 67 | + Utilities.writeFile(outPath, content); |
| 68 | + console.log(texts.updateSuccess(value.out)); |
| 69 | + } |
| 70 | + }); |
| 71 | +})(); |
0 commit comments