Skip to content

Commit 4ebf34d

Browse files
authored
Merge pull request #57 from MobileFirstLLC/31-sync
Change command to prompt with options
2 parents 63483e6 + 7a65245 commit 4ebf34d

File tree

4 files changed

+68
-85
lines changed

4 files changed

+68
-85
lines changed

cli/xt-sync.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @description
99
*
1010
* ```text
11-
* xt-sync [--gitignore] [--eslint] [--gitlab] [--travis] [--all]
11+
* xt-sync
1212
* ```
1313
*
1414
* The purpose of this command is to upgrade configuration files of
@@ -19,42 +19,53 @@
1919
*/
2020

2121
const path = require('path');
22+
const prompts = require('prompts');
2223
const program = require('commander');
2324
const pkg = require('../package.json');
2425
const texts = require('../config/texts').xtSync;
2526
const Utilities = require('./utilities').Utilities;
2627

28+
// list available options
2729
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'}
3234
};
3335

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+
3447
program
48+
.name('xt-sync')
49+
.option('-a --all', 'deprecated: call xt-sync without flags')
3550
.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)
4151
.parse(process.argv);
4252

43-
let counter = 0;
53+
(async () => {
4454

45-
const options = program.opts();
55+
const onCancel = () => process.exit(0)
56+
// noinspection JSUnresolvedVariable
57+
const response = (await prompts(options, {onCancel})).options;
4658

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]) => {
5361

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);
5966

60-
if (!counter) console.log(texts.onError);
67+
Utilities.writeFile(outPath, content);
68+
console.log(texts.updateSuccess(value.out));
69+
}
70+
});
71+
})();

config/init/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"clean": "xt-clean",
1717
"docs": "xt-docs",
1818
"test": "xt-test",
19-
"sync": "xt-sync --all"
19+
"sync": "xt-sync"
2020
},
2121
"babel": {
2222
"presets": [

config/texts.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,15 @@ exports.xtCreate = {
5555
*/
5656
exports.xtSync = {
5757

58-
argGitlab: 'sync gitlab-ci.yml',
58+
argGitlab: 'gitlab CI config',
5959

60-
argTravis: 'sync travis.yml',
60+
argTravis: 'travis CI config',
6161

62-
argLint: 'sync eslint',
62+
argLint: 'eslint config',
6363

64-
argGitIgnore: 'sync gitignore',
64+
argGitIgnore: 'gitignore',
6565

66-
argAll: 'sync everything',
67-
68-
onError: (
69-
chalk.red('Specify which files to sync using flags.\n' +
70-
'See --help for more details.')
71-
),
66+
instructions: 'choose the files you want to sync:',
7267

7368
updateSuccess: (what) => (
7469
chalk.bold.green(`✓ updated ${what}`)

guide/06-xt-sync.md

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,53 @@
33

44
* * *
55

6-
<p class='page-intro'><code>xt-sync</code> command is intended for upgrading configuration files of
7-
a stale project to the latest versions</p>
6+
<p class='page-intro'><code>xt-sync</code> is intended for copying and updating
7+
configuration files.</p>
88

99
* * *
1010

11-
When a project has not been worked on recently, it may need updates of
12-
various files, such as CI configuration files.
11+
When adding more features to an extension project, it may be helpful
12+
to not start from scratch. `xt-sync` command enables extension projects
13+
to pull in starter configuration files for the purposes of linting,
14+
setting up automated CI builds, and for setting up git VCS.
1315

14-
The purpose of this command is to make that update process simple by
15-
allowing each project to choose which configuration files to update.
16-
The CLI will then supply the project with the most recent configuration
17-
files.
16+
The configuration files are intended as a starting point. If you
17+
end up modifying them heavily at a project level, you should continue
18+
to maintain them manually instead of using this command.
1819

19-
**Note:** If the configuration files have been modified heavily for
20-
an individual project, it is not advisable to upgrade them in this manner.
21-
Instead you should upgrade such configuration files manually.
20+
If you do not modify these configuration files, you can sync the
21+
latest version periodically, to update to the latest version supplied
22+
by this CLI.
2223

2324
## Commands
2425

25-
Braces `{ }` indicate that the user must choose one (and only one) of the items inside the braces.
26+
**Sync configuration files**
2627

27-
You must pass at least one flag with this command.
28-
29-
30-
**Synchronize all configuration files**
28+
This command will guide you through the available options.
3129

3230
```bash
33-
xt-sync {-a|--all}
31+
xt-sync
3432
```
3533

36-
**Synchronize ESLint configuration file**
37-
38-
```bash
39-
xt-sync {-e|--eslint}
40-
```
4134

42-
**Synchronize Gitlab CI configuration file**
35+
## Package.json scripts
4336

37+
After installing extension-cli, you can run these commands from a terminal by calling
38+
4439
```bash
45-
xt-sync {-l|--gitlab}
40+
npx xt-sync
4641
```
4742

48-
**Synchronize Travis CI configuration file**
49-
50-
```bash
51-
xt-sync {-t|--travis}
52-
```
53-
54-
**Synchronize .gitignore file**
55-
56-
```bash
57-
xt-sync {-g|--gitignore}
58-
```
59-
60-
**Get help using this command**
61-
62-
```bash
63-
xt-sync --help
64-
```
65-
66-
## Package.json scripts
67-
68-
After installing extension-cli, you can run these commands from a terminal using `npx xt-sync --all`.
69-
70-
Or you can add an option to `packages.json` scripts section and then execute the command as `npm run sync`.
71-
See example below.
43+
Alternatively you can add an option to `packages.json` scripts section as shown below
7244

7345
```json
74-
"scripts":{
75-
"sync": "xt-sync --all"
46+
"scripts" : {
47+
"sync": "xt-sync"
7648
}
7749
```
7850

51+
and then execute the command by running
52+
53+
```bash
54+
npm run sync
55+
```

0 commit comments

Comments
 (0)