This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
forked from carafe-fm/carafe-bundle-create
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·156 lines (138 loc) · 4.38 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#! /usr/bin/env node
const fs = require('fs');
const path = require('path');
const meow = require('meow');
const chalk = require('chalk');
const prompts = require('prompts');
const semver = require('semver');
const slugify = require('slugify');
const uuidv4 = require('uuid/v4');
const copyDir = require('copy-dir');
const {exec} = require('child_process');
const logo = chalk.magenta('[carafe-bundle]');
const bundleDirNamePattern = /^[\w\-]+$/;
const slugifyOptions = { remove: /[^\w\s]/, replacement: '-', lower: true };
const log = (...args) => {
console.log(logo, ...args)
};
log.error = (...args) => {
console.log(chalk.red('[ERROR]'), ...args)
};
const cli = meow(
`
Usage
$ npm init @carafe-fm/bundle [my-new-bundle-name|-v|--version|-h|--help]
`,
{
booleanDefault: undefined,
flags: {
help: {
type: 'boolean',
alias: 'h',
},
version: {
type: 'boolean',
alias: 'v',
},
},
}
);
(async () => {
let [bundleName] = cli.input;
const questions = [
{
type: !bundleName ? 'text' : null,
name: 'bundleName',
message: 'Bundle Name?'
},
{
type: 'text',
name: 'bundleDirName',
message: 'Bundle Directory Name?',
initial: (prev, values) => slugify(bundleName ? bundleName : values.bundleName, slugifyOptions),
validate: bundleDirName => bundleDirNamePattern.test(bundleDirName) ? true : `Letters, numbers, underscores, hypens only`
},
{
type: 'autocomplete',
name: 'bundleCategory',
message: 'Bundle Category?',
initial: 'Development',
choices: [
{ title: 'Analysis' },
{ title: 'Calendar' },
{ title: 'Chart' },
{ title: 'Data Presentation' },
{ title: 'Date' },
{ title: 'Development' },
{ title: 'Hierarchy' },
{ title: 'Image Viewer' },
{ title: 'Map' },
{ title: 'Pivot' },
{ title: 'Progress Bar' },
{ title: 'Slider' },
{ title: 'Table' },
{ title: 'Text' },
{ title: 'Time' },
{ title: 'Tree' },
{ title: 'UI' },
{ title: 'Utility' }
],
limit: 5
},
{
type: 'text',
name: 'bundleCreator',
message: 'Bundle Creator?',
initial: process.env.USER
},
{
type: 'text',
name: 'bundleVersion',
message: 'Bundle Version?',
initial: '0.0.1',
validate: bundleVersion => carafeSemver(bundleVersion) ? true : `Not a valid version. Use MAJOR.MINOR.PATCH`
}
]
const response = await prompts(questions);
bundleName = response.bundleName ? response.bundleName : bundleName;
const root = extractTemplate(response.bundleDirName);
const metaPath = root + '/src/meta.json';
if (!fs.existsSync(metaPath)) {
log.error('Error while reading meta.json');
return;
}
const meta = JSON.parse(fs.readFileSync(metaPath));
meta.id = uuidv4();
meta.name = bundleName;
meta.version = response.bundleVersion;
meta.category = response.bundleCategory ? response.bundleCategory : 'Development';
meta.creator = response.bundleCreator;
fs.writeFileSync(metaPath, JSON.stringify(meta, null, 4));
installPackages(root);
})();
function carafeSemver(bundleVersion) {
return semver.valid(bundleVersion) && semver.valid(semver.coerce(bundleVersion)) === bundleVersion
};
function extractTemplate(name) {
const root = path.resolve(name);
if (fs.existsSync(root)) {
log.error('Directory already exists');
process.exit(1);
}
fs.mkdirSync(root, { recursive: true });
copyDir.sync(__dirname + '/template', root);
fs.renameSync(root + '/dist.gitignore', root + '/.gitignore');
return root;
}
function installPackages(root) {
log('Installing packages…');
exec('npm install', {
cwd: root,
}, (err, stdout, stderr) => {
if (err) {
log.error('Error while installing packages');
log.error(stderr);
}
log('Done installing packages');
});
}