forked from b12io/kinase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kinase.js
83 lines (68 loc) · 2.29 KB
/
Kinase.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
const cloneDeep = require('lodash.clonedeep');
const fs = require('fs');
const merge = require('lodash.merge');
const path = require('path');
const shell = require('shelljs');
const tmp = require('tmp');
const manifestJSON = require('./manifest.json');
const packageJSON = require('./package.json');
shell.set('-e');
module.exports = class Kinase {
constructor(options) {
this.options = options;
tmp.dir((err, tmpPath) => {
if (err) throw err;
if (!this.options.output) {
throw Error('You must provide an output path for the packaged extension.');
}
this.tmpPath = tmpPath;
// Copy extension files into temporary directory
shell.exec(`cp -r ${__dirname}/. ${this.tmpPath}/`, {
cwd: __dirname,
});
// Update manifest first so the new extension name is used during build
this.updateManifest();
this.buildExtension();
this.packageExtension();
}, {
unsafeCleanup: true,
});
}
buildExtension() {
if (this.options.apiFile) {
// Copy custom API file into temporary directory to add to webpack pipeline
console.log('\nAdding custom API...');
shell.cp(this.options.apiFile, path.join(this.tmpPath, 'src/api.js'));
if (this.options.dependencies) {
// Add API requirements to base package.json
const newPackage = merge({}, packageJSON, {
dependencies: this.options.dependencies,
});
fs.writeFileSync(
path.join(this.tmpPath, 'package.json'),
`${JSON.stringify(newPackage, null, 2)}\n`);
}
}
// Build extension with new manifest and API
shell.exec('yarn && webpack && rm -rf node_modules/', {
cwd: this.tmpPath,
});
}
updateManifest() {
let newManifest = cloneDeep(manifestJSON);
if (this.options.manifestOverrides) {
console.log('\nOverriding manifest defaults...');
newManifest = merge(newManifest, this.options.manifestOverrides || {});
}
fs.writeFileSync(
path.join(this.tmpPath, 'manifest.json'),
`${JSON.stringify(newManifest, null, 2)}\n`);
}
packageExtension() {
console.log('\nZipping up extension...');
shell.mkdir('-p', path.dirname(this.options.output));
shell.exec(`zip -qr ${this.options.output} .`, {
cwd: this.tmpPath,
});
}
};