@cxl/build 0.0.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/file.js ADDED
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MinifyDefault = void 0;
4
+ exports.ls = ls;
5
+ exports.read = read;
6
+ exports.filterPath = filterPath;
7
+ exports.file = file;
8
+ exports.basename = basename;
9
+ exports.concatFile = concatFile;
10
+ exports.files = files;
11
+ exports.matchStat = matchStat;
12
+ exports.copyDir = copyDir;
13
+ exports.getSourceMap = getSourceMap;
14
+ exports.minify = minify;
15
+ exports.minifyDir = minifyDir;
16
+ exports.zip = zip;
17
+ const Terser = require("terser");
18
+ const rx_1 = require("@cxl/rx");
19
+ const fs_1 = require("fs");
20
+ const path_1 = require("path");
21
+ const builder_js_1 = require("./builder.js");
22
+ function ls(dir) {
23
+ return new rx_1.Observable(async (subs) => {
24
+ const files = await fs_1.promises.readdir(dir);
25
+ for (const path of files)
26
+ subs.next((0, path_1.resolve)(dir, path));
27
+ subs.complete();
28
+ });
29
+ }
30
+ async function read(source) {
31
+ const content = await fs_1.promises.readFile(source);
32
+ return {
33
+ path: source,
34
+ source: content,
35
+ };
36
+ }
37
+ function filterPath(matchPath) {
38
+ matchPath = (0, path_1.resolve)(matchPath);
39
+ return (0, rx_1.filter)((out) => (0, path_1.resolve)(out.path).startsWith(matchPath));
40
+ }
41
+ function file(source, out) {
42
+ return (0, rx_1.defer)(() => (0, rx_1.from)(read(source).then(res => ({
43
+ path: out || (0, path_1.resolve)(source),
44
+ source: res.source,
45
+ }))));
46
+ }
47
+ function basename(replace) {
48
+ return (0, rx_1.tap)(out => (out.path = (replace || '') + (0, path_1.basename)(out.path)));
49
+ }
50
+ function concatFile(outName, separator = '\n') {
51
+ return (0, rx_1.pipe)((0, rx_1.reduce)((out, src) => `${out}${separator}${src.source}`, ''), (0, rx_1.map)(source => ({ path: outName, source: Buffer.from(source) })));
52
+ }
53
+ function files(sources) {
54
+ return new rx_1.Observable(subs => {
55
+ Promise.all(sources.map(read)).then(out => {
56
+ out.forEach(o => subs.next(o));
57
+ subs.complete();
58
+ }, e => subs.error(e));
59
+ });
60
+ }
61
+ function matchStat(fromPath, toPath) {
62
+ return Promise.all([fs_1.promises.stat(fromPath), fs_1.promises.stat(toPath)]).then(([fromStat, toStat]) => fromStat.mtime.getTime() === toStat.mtime.getTime(), () => true);
63
+ }
64
+ function copyDir(fromPath, toPath, glob = '*') {
65
+ return (0, builder_js_1.exec)(`mkdir -p ${toPath} && rsync -au -i --delete ${fromPath}/${glob} ${toPath}`);
66
+ }
67
+ function getSourceMap(out) {
68
+ const source = out.source.toString();
69
+ const match = /\/\/# sourceMappingURL=(.+)/.exec(source);
70
+ const path = match ? (0, path_1.resolve)((0, path_1.dirname)(out.path), match?.[1]) : null;
71
+ if (path)
72
+ return { path: (0, path_1.basename)(path), source: (0, fs_1.readFileSync)(path) };
73
+ }
74
+ exports.MinifyDefault = {
75
+ ecma: 2020,
76
+ };
77
+ function minify(op) {
78
+ return (0, rx_1.mergeMap)(out => {
79
+ const config = { ...exports.MinifyDefault, ...op };
80
+ if (!out.path.endsWith('.js'))
81
+ return (0, rx_1.of)(out);
82
+ const destPath = op?.changePath === false
83
+ ? out.path
84
+ : out.path.replace(/\.js$/, '.min.js');
85
+ if (config.sourceMap === undefined) {
86
+ const sourceMap = getSourceMap(out);
87
+ if (sourceMap)
88
+ config.sourceMap = {
89
+ content: sourceMap.source.toString(),
90
+ url: destPath + '.map',
91
+ };
92
+ }
93
+ const source = out.source.toString();
94
+ delete config.changePath;
95
+ return new rx_1.Observable(async (subscriber) => {
96
+ try {
97
+ const { code, map } = await Terser.minify({ [out.path]: source }, config);
98
+ if (!code)
99
+ throw new Error('No code generated');
100
+ subscriber.next({
101
+ path: destPath,
102
+ source: Buffer.from(code),
103
+ });
104
+ if (map && config.sourceMap)
105
+ subscriber.next({
106
+ path: config.sourceMap.url,
107
+ source: Buffer.from(map.toString()),
108
+ });
109
+ }
110
+ catch (e) {
111
+ console.error(`Error minifying ${out.path}`);
112
+ throw e;
113
+ }
114
+ subscriber.complete();
115
+ });
116
+ });
117
+ }
118
+ function minifyDir(dir, op) {
119
+ return ls(dir)
120
+ .filter(path => path.endsWith('.js'))
121
+ .mergeMap(file)
122
+ .pipe(minify(op));
123
+ }
124
+ function zip(src, path) {
125
+ return (0, builder_js_1.shell)(`zip - ${src.join(' ')}`).map(source => ({ path, source }));
126
+ }
package/git.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function getBranch(cwd: string): Promise<string>;
2
+ export declare function checkBranchClean(branch: string): Promise<void>;
3
+ export declare function checkBranchUpToDate(branch?: string): Promise<void>;
package/git.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBranch = getBranch;
4
+ exports.checkBranchClean = checkBranchClean;
5
+ exports.checkBranchUpToDate = checkBranchUpToDate;
6
+ const program_1 = require("@cxl/program");
7
+ async function getBranch(cwd) {
8
+ return (await (0, program_1.sh)('git rev-parse --abbrev-ref HEAD', { cwd })).trim();
9
+ }
10
+ async function checkBranchClean(branch) {
11
+ try {
12
+ await (0, program_1.sh)(`git status > /dev/null; git diff-index --quiet ${branch}`);
13
+ }
14
+ catch (e) {
15
+ throw new Error('Not a clean repository');
16
+ }
17
+ }
18
+ async function checkBranchUpToDate(branch = 'master') {
19
+ try {
20
+ await (0, program_1.sh)(`git diff origin ${branch} --quiet`);
21
+ }
22
+ catch (e) {
23
+ throw new Error('Branch has not been merged with origin');
24
+ }
25
+ }
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+ export { tsconfig, bundle as tsBundle } from './tsc.js';
3
+ export { basename, file, files, concatFile, copyDir, minify, zip, } from './file.js';
4
+ export { concat } from '@cxl/rx';
5
+ export { mkdirp, sh, readJson } from '@cxl/program';
6
+ export { Output } from '@cxl/source';
7
+ export { AMD, REQUIRE_REPLACE, pkg, readme, bundle, bundleAmd, template, docgen, docgenTask, } from './package.js';
8
+ export { Package } from './npm.js';
9
+ export { buildCxl } from './cxl.js';
10
+ export { Task, build, exec, shell } from './builder.js';
package/index.js CHANGED
@@ -1,285 +1,40 @@
1
- /*jshint node:true */
2
- const
3
- fs = require('fs'),
4
- cp = require('child_process'),
5
- path = require('path'),
6
- colors = require('colors'),
7
- UglifyJS = require('uglify-es'),
8
-
9
- SCRIPTDIR = path.dirname(process.argv[1]),
10
- BASEDIR = cp.execSync(`npm prefix`, { cwd: SCRIPTDIR }).toString().trim(),
11
- ARGV = process.argv.slice(2).reduce(
12
- (acc, cur) => { acc[cur]=true; return acc; }, {}),
13
- PACKAGE = require(BASEDIR + '/package.json'),
14
- CONFIG = {
15
- package: PACKAGE
16
- }
17
- ;
18
-
19
- console.log(`Running in ${BASEDIR}`);
20
- process.chdir(BASEDIR);
21
-
22
- function hrtime()
23
- {
24
- var time = process.hrtime();
25
- return time[0] + (time[1]/1e9);
26
- }
27
-
28
- function formatTime(time, time2)
29
- {
30
- if (time2===undefined)
31
- time2 = hrtime();
32
- const
33
- s = time2-time,
34
- str = s.toFixed(4) + 's'
35
- ;
36
- // Color code based on time,
37
- return s > 0.1 ? (s > 0.5 ? colors.red(str) : colors.yellow(str)) : str;
38
- }
39
-
40
- function readFile(filename, encoding)
41
- {
42
- return new Promise((resolve, reject) =>
43
- {
44
- fs.readFile(filename, encoding, (err, content) =>
45
- {
46
- if (err) return reject(err);
47
- resolve(content);
48
- });
49
- });
50
- }
51
-
52
- function read(filename, encoding)
53
- {
54
- return Array.isArray(filename) ?
55
- Promise.all(filename.map(f => readFile(f, encoding))) :
56
- readFile(filename, encoding);
57
- }
58
-
59
- function write(filename, content)
60
- {
61
- return new Promise(function(resolve, reject) {
62
- fs.writeFile(filename, content, err => {
63
- if (err) return reject(err);
64
- resolve();
65
- });
66
- });
67
- }
68
-
69
- function $stat(filename)
70
- {
71
- return new Promise(function(resolve, reject) {
72
- fs.stat(filename, function(err, stat) {
73
- if (err) return reject(err);
74
- resolve(stat);
75
- });
76
- });
77
- }
78
-
79
- function kb(bytes)
80
- {
81
- return (bytes/1000).toFixed(2);
82
- }
83
-
84
- function stat(file)
85
- {
86
- return $stat(file).catch(() => ({size:0}));
87
- }
88
-
89
- class Operation {
90
-
91
- constructor(msg, fn)
92
- {
93
- this.start = () => {
94
- const
95
- t = hrtime(),
96
- result = typeof(fn)==='function' ? fn() : fn,
97
- done = () => this.log(`${msg} (${formatTime(t)})`)
98
- ;
99
- if (result && result.then)
100
- return result.then(function(res) {
101
- done();
102
- return res;
103
- });
104
- else
105
- done();
106
-
107
- return result;
108
- };
109
- }
110
-
111
- error(msg)
112
- {
113
- console.error(colors.red(`${this.prefix} ${msg}`));
114
- process.exit(1);
115
- }
116
-
117
- log(msg)
118
- {
119
- console.log(`${this.prefix} ${msg}`);
120
- }
121
-
122
- }
123
-
124
- class Task {
125
-
126
- constructor(name, fn)
127
- {
128
- this.name = name;
129
- this.fn = fn;
130
- }
131
-
132
- }
133
-
134
- class Builder {
135
-
136
- static build(config)
137
- {
138
- var builder = new Builder();
139
-
140
- return builder.buildAll(config);
141
- }
142
-
143
- error(msg)
144
- {
145
- console.error(colors.red(`${this.prefix} ${msg}`));
146
- process.exit(1);
147
- }
148
-
149
- log(msg)
150
- {
151
- console.log(`${this.prefix} ${msg}`);
152
- }
153
-
154
- operation(msg, fn, scope)
155
- {
156
- var
157
- t = hrtime(),
158
- result = typeof(fn)==='function' ? fn.call(scope) : fn,
159
- done = () => this.log(`${msg} (${formatTime(t)})`)
160
- ;
161
- if (result && result.then)
162
- result = result.then(function(res) {
163
- done();
164
- return res;
165
- });
166
- else
167
- done();
168
-
169
- return result;
170
- }
171
-
172
- buildAll(config)
173
- {
174
- this.prefix = config.name || 'build';
175
- this.outputDir = config.outputDir || 'dist';
176
- this.config = config;
177
- this.reportOutput = { targets: {} };
178
-
179
- try { fs.mkdirSync(this.outputDir); } catch(e) {}
180
-
181
- Promise.all(config.targets.map(
182
- target => this.operation(`Building ${target.output}`, this.build(target))
183
- )).then(() => {
184
- }, err => this.error(err));
185
- }
186
-
187
- report(old, config)
188
- {
189
- const output = this.reportOutput.targets;
190
-
191
- return this.stat(config).then(stats => {
192
- console.log(config.output + `: ${kb(old[0].size)}Kb -> ${kb(stats[0].size)}Kb`);
193
- output[config.output] = stats[0].size;
194
-
195
- if (ARGV.minify && config.minify)
196
- {
197
- console.log(config.minify + ': ' + kb(stats[1].size) + 'Kb (' +
198
- (stats[1].size/stats[0].size*100).toFixed(2) + '%)');
199
- output[config.minify] = stats[1].size;
200
- }
201
- });
202
- }
203
-
204
- minify(source, output)
205
- {
206
- return this.operation('Minifying', () => {
207
- const result = UglifyJS.minify(source, {
208
- sourceMap: false,
209
- toplevel: true,
210
- output: { ascii_only: true }
211
- });
212
-
213
- if (result.error)
214
- throw new Error(result.error);
215
-
216
- return write(output, result.code);
217
- });
218
- }
219
-
220
- stat(config)
221
- {
222
- return Promise.all([
223
- stat(this.outputDir + '/' + config.output),
224
- stat(this.outputDir + '/' + config.minify)
225
- ]);
226
- }
227
-
228
- processSource(source)
229
- {
230
- const type = typeof(source);
231
-
232
- if (type==='function')
233
- return source(CONFIG);
234
-
235
- return read(source, 'utf8');
236
- }
237
-
238
- async readSource(src)
239
- {
240
- if (!Array.isArray(src))
241
- src = [ src ];
242
-
243
- return Promise.all(src.map(this.processSource));
244
- }
245
-
246
- async build(target)
247
- {
248
- const
249
- oldStat = await this.stat(target),
250
- source = (await this.readSource(target.src)).join("\n")
251
- ;
252
- await write(this.outputDir + '/' + target.output, source);
253
-
254
- if (ARGV.minify && target.minify)
255
- await this.minify(source, this.outputDir + '/' + target.minify);
256
-
257
- this.report(oldStat, target);
258
- }
259
-
260
- }
261
-
262
- Object.assign(Builder, {
263
- read: read,
264
- stat: $stat,
265
- write: write,
266
-
267
- copy(src, dest)
268
- {
269
- if (Array.isArray(src))
270
- return Promise.all(src.map(s => this.copy(s, dest)));
271
-
272
- return new Promise((resolve, reject) => {
273
- fs.copyFile(src, dest, err => err ? reject(err) : resolve());
274
- });
275
- },
276
-
277
- list(path)
278
- {
279
- return new Promise((resolve, reject) => {
280
- fs.readdir(path, (err, files) => err ? reject(err) : resolve(files));
281
- });
282
- }
283
- });
284
-
285
- module.exports = Builder;
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.shell = exports.exec = exports.build = exports.buildCxl = exports.docgenTask = exports.docgen = exports.template = exports.bundleAmd = exports.bundle = exports.readme = exports.pkg = exports.REQUIRE_REPLACE = exports.AMD = exports.readJson = exports.sh = exports.mkdirp = exports.concat = exports.zip = exports.minify = exports.copyDir = exports.concatFile = exports.files = exports.file = exports.basename = exports.tsBundle = exports.tsconfig = void 0;
5
+ const cxl_js_1 = require("./cxl.js");
6
+ var tsc_js_1 = require("./tsc.js");
7
+ Object.defineProperty(exports, "tsconfig", { enumerable: true, get: function () { return tsc_js_1.tsconfig; } });
8
+ Object.defineProperty(exports, "tsBundle", { enumerable: true, get: function () { return tsc_js_1.bundle; } });
9
+ var file_js_1 = require("./file.js");
10
+ Object.defineProperty(exports, "basename", { enumerable: true, get: function () { return file_js_1.basename; } });
11
+ Object.defineProperty(exports, "file", { enumerable: true, get: function () { return file_js_1.file; } });
12
+ Object.defineProperty(exports, "files", { enumerable: true, get: function () { return file_js_1.files; } });
13
+ Object.defineProperty(exports, "concatFile", { enumerable: true, get: function () { return file_js_1.concatFile; } });
14
+ Object.defineProperty(exports, "copyDir", { enumerable: true, get: function () { return file_js_1.copyDir; } });
15
+ Object.defineProperty(exports, "minify", { enumerable: true, get: function () { return file_js_1.minify; } });
16
+ Object.defineProperty(exports, "zip", { enumerable: true, get: function () { return file_js_1.zip; } });
17
+ var rx_1 = require("@cxl/rx");
18
+ Object.defineProperty(exports, "concat", { enumerable: true, get: function () { return rx_1.concat; } });
19
+ var program_1 = require("@cxl/program");
20
+ Object.defineProperty(exports, "mkdirp", { enumerable: true, get: function () { return program_1.mkdirp; } });
21
+ Object.defineProperty(exports, "sh", { enumerable: true, get: function () { return program_1.sh; } });
22
+ Object.defineProperty(exports, "readJson", { enumerable: true, get: function () { return program_1.readJson; } });
23
+ var package_js_1 = require("./package.js");
24
+ Object.defineProperty(exports, "AMD", { enumerable: true, get: function () { return package_js_1.AMD; } });
25
+ Object.defineProperty(exports, "REQUIRE_REPLACE", { enumerable: true, get: function () { return package_js_1.REQUIRE_REPLACE; } });
26
+ Object.defineProperty(exports, "pkg", { enumerable: true, get: function () { return package_js_1.pkg; } });
27
+ Object.defineProperty(exports, "readme", { enumerable: true, get: function () { return package_js_1.readme; } });
28
+ Object.defineProperty(exports, "bundle", { enumerable: true, get: function () { return package_js_1.bundle; } });
29
+ Object.defineProperty(exports, "bundleAmd", { enumerable: true, get: function () { return package_js_1.bundleAmd; } });
30
+ Object.defineProperty(exports, "template", { enumerable: true, get: function () { return package_js_1.template; } });
31
+ Object.defineProperty(exports, "docgen", { enumerable: true, get: function () { return package_js_1.docgen; } });
32
+ Object.defineProperty(exports, "docgenTask", { enumerable: true, get: function () { return package_js_1.docgenTask; } });
33
+ var cxl_js_2 = require("./cxl.js");
34
+ Object.defineProperty(exports, "buildCxl", { enumerable: true, get: function () { return cxl_js_2.buildCxl; } });
35
+ var builder_js_1 = require("./builder.js");
36
+ Object.defineProperty(exports, "build", { enumerable: true, get: function () { return builder_js_1.build; } });
37
+ Object.defineProperty(exports, "exec", { enumerable: true, get: function () { return builder_js_1.exec; } });
38
+ Object.defineProperty(exports, "shell", { enumerable: true, get: function () { return builder_js_1.shell; } });
39
+ if (require.main?.filename === __filename)
40
+ (0, cxl_js_1.buildCxl)();