This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-self.ts
91 lines (87 loc) · 3.32 KB
/
update-self.ts
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
import { BufferFile } from "vinyl";
import { Stream } from "stream";
(function() {
const
spawn = requireModule<Spawn>("spawn"),
gulp = requireModule<GulpWithHelp>("gulp"),
env = requireModule<Env>("env"),
chalk = requireModule<AnsiColors>("ansi-colors"),
resolveMasks = requireModule<ResolveMasks>("resolve-masks"),
debug = requireModule<DebugFactory>("debug")(__filename),
es = require("event-stream");
env.associate([
env.DRY_RUN,
env.INCLUDE_PACKAGE_JSON,
env.EXCLUDE_PACKAGE_JSON
], "update-self");
gulp.task(
"update-self",
"Updates zarro throughout your current project",
() => {
const
glob = resolveMasks(env.INCLUDE_PACKAGE_JSON, env.EXCLUDE_PACKAGE_JSON);
debug({
glob
});
return gulp.src(glob)
.pipe(updateZarroPipe(env.resolveFlag(env.BETA)));
}
);
function updateZarroPipe(beta: boolean) {
const
promises: Promise<any>[] = [];
return es.through(function input(this: Stream, file: BufferFile) {
let
save = false,
saveDev = false;
const json = file.contents.toString();
try {
const
search = "zarro",
packageIndex = JSON.parse(json) as PackageIndex,
deps = packageIndex.dependencies || {},
devDeps = packageIndex.devDependencies || {};
save = Object.keys(deps).includes(search);
saveDev = Object.keys(devDeps).includes(search);
if (!save && !saveDev) {
debug(`${ search } not installed in ${ file.path }`);
return;
}
} catch (e) {
debug(`${ file.path } is not valid JSON`);
return;
}
console.log(chalk.yellow(`update zarro in: ${ file.dirname }`));
const
proc = "npm",
tag = beta ? "beta" : "latest",
args = [ "install", save ? "--save" : "--save-dev", `zarro@${ tag }`, "--no-progress", "--silent" ],
opts = {
cwd: file.dirname,
interactive: true
}
if (env.resolveFlag(env.DRY_RUN)) {
console.log({
label: "would run spawn with",
proc,
args,
opts
});
} else {
promises.push(spawn.call(null, proc, args, opts))
}
}, async function end(this: Stream) {
try {
await Promise.all(promises)
this.emit("end");
} catch (err) {
console.error(chalk.redBright(`
==================================================
| WARNING: Unable to update zarro, error(s) follow |
==================================================
`));
this.emit("error", err);
}
});
}
})();