-
Notifications
You must be signed in to change notification settings - Fork 27
/
createPackage.js
90 lines (73 loc) · 2.61 KB
/
createPackage.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
const childProcess = require('child_process');
const fs = require('fs-extra');
const stringify = require('json-stringify-pretty-compact');
const rimraf = require('rimraf');
if (!process.argv[4]) {
/*eslint-disable */
console.log(`
usage: node createPackage.js <activity|operator> <short-name> <title>
eg: To create an activity -> node createPackage.js activity ac-new-activity "A new activity"
Please stop Meteor before running this command (otherwise this command will crash Meteor).
Features:
* Sets up a simple activity or operator package template in ./[ac|op]/<short-name>.
* Also does the correct symlinking to be ready to develop.`);
/* eslint-enable */
process.exit();
}
const type = process.argv[2];
const prefix = type === 'activity' ? 'ac' : 'op';
if (process.argv[3].slice(0, 3) !== prefix + '-') {
/*eslint-disable */
console.log(`
activityPackage names should start by 'ac-...' and operatorPackage names with 'op-...'.
`);
/* eslint-enable */
process.exit();
}
const newActivityId = process.argv[3];
if (fs.existsSync(`./${prefix}/${newActivityId}/package.json`)) {
// eslint-disable-next-line no-console
console.log(`Package already exists.`);
process.exit();
}
fs.copySync(`./templates/${type}`, `./${prefix}/${newActivityId}`, {
errorOnExist: true
});
// adding to ac/ac-new/package.json
const newpkgjs = fs.readFileSync(`./${prefix}/${newActivityId}/package.json`);
const newpkg = JSON.parse(newpkgjs);
newpkg.name = newActivityId;
fs.writeFileSync(
`./${prefix}/${newActivityId}/package.json`,
stringify(newpkg)
);
// modifying template src/index.js
const actnew = fs
.readFileSync(`./${prefix}/${newActivityId}/src/index.js`)
.toString()
.split('\n');
const pos = actnew.findIndex(x => x.startsWith(' id:'));
actnew.splice(pos, 1, ` id: '${newActivityId}',`);
const pos1 = actnew.findIndex(x => x.startsWith(' name:'));
actnew.splice(pos1, 1, ` name: '${process.argv[4]}',`);
fs.writeFileSync(
`./${prefix}/${newActivityId}/src/index.js`,
actnew.join('\n')
);
childProcess.execSync(`ln -s ../${prefix}/${newActivityId} ./node_modules/`);
childProcess.execSync(
`ln -s ../../node_modules/${newActivityId} ./frog/imports/packages/`
);
childProcess.execSync(
`git add ./${prefix}/${newActivityId} ./frog/imports/packages/${newActivityId}`
);
[
'./frog/.meteor/local/build',
'./frog/.meteor/local/bundler-cache',
'./frog/.meteor/local/plugin-cache'
].forEach(x => rimraf.sync(x));
/*eslint-disable */
console.info(
`Package created in './${prefix}/${newActivityId}', and added to ./frog.
Use 'git diff --cached' to see all the changes that the script has made.`
);