-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
110 lines (80 loc) · 3.01 KB
/
setup.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
"use strict"
const fs = require("fs");
const exec = require("child_process").exec;
const spawn = require("child_process").spawn;
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
const mode = "inherit";
let PLUGIN_NAME = "au-datetime-picker";
let PLUGIN_SAMPLE_NAME = "au-datetime-picker-sample";
let PLUGIN_VERSION = "0.0.1";
function spawner(cmd, args, dirname) {
return new Promise((resolve, reject) => {
var childSpawn = spawn(cmd, args, {
stdio: mode,
cwd: dirname
});
childSpawn.on("exit", function (code) {
if (code != 0) {
console.log("Failed: " + code);
reject();
} else {
resolve()
}
});
});
}
this.mainPath = `${__dirname}${process.platform === "win32" ? "\\" : "//"}`
this.samplePath = `${__dirname}${process.platform === "win32" ? "\\sample" : "//sample"}`
function safeIncreaseVersion(version) {
let theVersion = parseInt(version);
if (theVersion >= Number.MAX_SAFE_INTEGER - 1) {
console.warn('First delete existing yarn,npm,node,fuse-box,webpack');
return 0;
} else {
return theVersion + 1;
}
}
function updateSampleConfig() {
console.info('Updating plugin package.json...');
const argVersion = process.argv[2]; // -v
const argVersionNumber = process.argv[3]; // eg 1.5.6
let pluginPackageFile = './package.json';
let samplePackageFile = './sample/package.json';
fs.readFile(pluginPackageFile, 'utf8', (err, data) => {
let obj = JSON.parse(data);
let versions = new Array();
versions = obj.version.split('.');
PLUGIN_NAME = obj.name || PLUGIN_NAME;
PLUGIN_VERSION = obj.version || PLUGIN_VERSION;
if (versions && (versions.length > 0)) {
if (argVersion != undefined && argVersionNumber == undefined && (argVersion == '-v' || argVersion == '--Version')) {
versions[versions.length - 1] = safeIncreaseVersion(versions[versions.length - 1]);
}
if (argVersion != undefined && argVersionNumber != undefined && (argVersion == '-v' || argVersion == '--Version')) {
versions = argVersionNumber.split('.');
}
console.info(`Version changing ${obj.version} => ${versions.join('.')}`);
obj.version = versions.join('.');
PLUGIN_VERSION = versions.join('.');
}
const dev = obj.dependencies;
let sampleDep = {};
let sample = {};
fs.readFile(samplePackageFile, 'utf8', (err, res) => {
sample = JSON.parse(res);
sampleDep = sample.dependencies;
sample.dependencies = dev;
sample.version = PLUGIN_VERSION;
sample.name = PLUGIN_SAMPLE_NAME;
obj = JSON.stringify(obj, null, 4);
let smplObj = JSON.stringify(sample, null, 4);
fs.writeFile(pluginPackageFile, obj, function (e) { });
fs.writeFile(samplePackageFile, smplObj, function (e) { });
console.log('Plugin package.json updated.');
});
});
}
let NpmInstallRoot = spawner(npm, ["install"], this.mainPath).then(() => {
updateSampleConfig();
let NpmInstallSample = spawner(npm, ["install"], this.samplePath);
});