This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
sparky_chain.ts
147 lines (142 loc) · 4.53 KB
/
sparky_chain.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
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
import * as path from 'path';
import { env } from '../env';
import { FuseBoxLogAdapter } from '../fuseLog/FuseBoxLogAdapter';
import {
ensureAbsolutePath,
ensureDir,
ensureFuseBoxPath,
path2RegexPattern,
readFile,
readFileAsBuffer,
removeFile,
writeFile,
} from '../utils/utils';
import { bumpVersion, IBumpVersion } from './bumpVersion';
import { sparky_src } from './sparky_src';
import { TscOptions, tsc } from './tsc';
export interface ISparkyChain {
bumpVersion: (mask: RegExp | string, opts: IBumpVersion) => ISparkyChain;
clean: () => ISparkyChain;
contentsOf: (mask: RegExp | string, fn: (contents: string) => string) => ISparkyChain;
dest: (target: string, base: string) => ISparkyChain;
exec: () => Promise<Array<string>>;
filter: (a: ((file: string) => any) | RegExp) => ISparkyChain;
src: (glob: string) => ISparkyChain;
tsc: (opts: TscOptions) => ISparkyChain;
write: () => ISparkyChain;
}
export function sparkyChain(log: FuseBoxLogAdapter): ISparkyChain {
const activities = [];
const readFiles = {};
let newLocation, newLocationBase;
const runActivities = async () => {
let latest: any;
for (const fn of activities) {
latest = await fn(latest);
}
if (newLocation && newLocationBase) {
const root = ensureAbsolutePath(newLocation, env.SCRIPT_PATH);
for (const i in latest) {
const file = latest[i];
readFiles[file] = readFiles[file] || readFileAsBuffer(file);
// normalize path so split works with windows path
const s = path.normalize(ensureFuseBoxPath(file)).split(newLocationBase);
if (!s[1]) {
log.error(`Can't find base of ${newLocationBase} of ${file}`);
return;
} else {
const newFileLocation = path.join(root, s[1]);
ensureDir(path.dirname(newFileLocation));
await writeFile(newFileLocation, readFiles[file]);
}
}
}
return latest;
};
const chain = {
__scope: () => {
return { readFiles };
},
// grabs the files by glob
src: (glob: string) => {
activities.push(() => sparky_src(glob));
return chain;
},
bumpVersion: (mask: RegExp | string, opts: IBumpVersion) => {
const re: RegExp = typeof mask === 'string' ? path2RegexPattern(mask) : mask;
activities.push(async (files: Array<string>) => {
const target = files.find(file => re.test(file));
if (target) {
readFiles[target] = readFiles[target] || readFile(target);
readFiles[target] = bumpVersion(readFiles[target], opts);
}
return files;
});
return chain;
},
clean: () => {
activities.push(async (files: Array<string>) => {
files.forEach(file => {
removeFile(file);
});
return files;
});
return chain;
},
// can be used to replace the contents of a file
contentsOf: (mask: RegExp | string, fn: (contents: string) => string) => {
const re: RegExp = typeof mask === 'string' ? path2RegexPattern(mask) : mask;
activities.push(async (files: Array<string>) => {
const target = files.find(file => re.test(file));
if (target) {
readFiles[target] = readFiles[target] || readFile(target);
readFiles[target] = fn(readFiles[target]);
}
return files;
});
return chain;
},
dest: (target: string, base: string) => {
newLocation = target;
newLocationBase = base;
return chain;
},
// runs all the activities (used mainly for testing)
exec: async () => runActivities(),
// filters out unwanted files
// accepts function and a regexp
filter: input => {
activities.push((files: Array<string>) => {
const filtered = files.filter(file => {
if (typeof input === 'function') {
return !!input(file);
} else if (input && typeof input.test === 'function') {
return input.test(file);
}
return true;
});
return filtered;
});
return chain;
},
tsc: (options?: TscOptions) => {
activities.push(async (files: Array<string>) => {
await tsc({ files: files, ...options });
});
return chain;
},
write: () => {
activities.push(async (files: Array<string>) => {
const _ = [];
for (const file in readFiles) {
_.push(writeFile(file, readFiles[file]));
}
return Promise.all(_).then(() => {
return files;
});
});
return chain;
},
};
return chain;
}