@cmmn/tools 1.3.0 → 1.4.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/bin.js CHANGED
@@ -1,19 +1,19 @@
1
- #!/usr/bin/env node
2
-
3
- import {bundle} from "./bundle/bundle.js";
4
- import {compile} from "./compile/compile.js";
5
- import {gen} from "./gen/gen.js";
6
-
7
- const [action, ...args] = process.argv.slice(2);
8
-
9
- const actions = {
10
- bundle, compile, gen
11
- }
12
-
13
- if (action in actions) {
14
- actions[action](...args);
15
- } else {
16
- console.log(`cmmn bundle [-b] [index.ts] [--watch] [--run] [--prod]`);
17
- console.log(`cmmn compile [-b] [--watch]`);
18
- console.log(`cmmn gen AppRoot . [--nested]`);
19
- }
1
+ #!/usr/bin/env node
2
+
3
+ import {bundle} from "./bundle/bundle.js";
4
+ import {compile} from "./compile/compile.js";
5
+ import {gen} from "./gen/gen.js";
6
+
7
+ const [action, ...args] = process.argv.slice(2);
8
+
9
+ const actions = {
10
+ bundle, compile, gen
11
+ }
12
+
13
+ if (action in actions) {
14
+ actions[action](...args);
15
+ } else {
16
+ console.log(`cmmn bundle [-b] [index.ts] [--watch] [--run] [--prod]`);
17
+ console.log(`cmmn compile [-b] [--watch]`);
18
+ console.log(`cmmn gen AppRoot . [--nested]`);
19
+ }
package/bundle/bundle.js CHANGED
@@ -1,116 +1,116 @@
1
- import {ConfigCreator} from "./rollup.config.js";
2
- import {rollup, watch} from "rollup";
3
- import fs from "fs";
4
- import path from "path";
5
- import fg from "fast-glob";
6
-
7
- function getProjectConfig(rootDir, cmmn, options) {
8
- const configCreator = new ConfigCreator({
9
- ...options,
10
- ...cmmn,
11
- });
12
- configCreator.setRootDir(rootDir);
13
- return configCreator.getConfig();
14
- }
15
-
16
- function getPackageConfigs(rootDir, options, name = null) {
17
- const results = [];
18
- const pkg = JSON.parse(fs.readFileSync(path.join(rootDir, 'package.json')));
19
- if (name) {
20
- results.push(...getProjectConfig(rootDir, pkg.cmmn[name], {
21
- ...options,
22
- name
23
- }));
24
- } else {
25
- for (let name in pkg.cmmn) {
26
- results.push(...getProjectConfig(rootDir, pkg.cmmn[name], {
27
- ...options,
28
- name
29
- }));
30
- }
31
- }
32
- return results;
33
- }
34
-
35
- function getLernaSubPackages(lernaFile, options) {
36
- const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
37
- const packages = config.packages;
38
- const dirs = packages.flatMap(pkg => fg.sync([pkg], {
39
- absolute: true,
40
- globstar: true,
41
- onlyDirectories: true,
42
- cwd: path.dirname(lernaFile)
43
- }));
44
- return dirs.flatMap(dir => getPackageConfigs(dir, options));
45
- }
46
-
47
- function getConfigs(options) {
48
- if (!options.input || options.project) {
49
- const rootDir = process.cwd();
50
- const lernaPath = path.join(rootDir, 'lerna.json');
51
- if (fs.existsSync(lernaPath)) {
52
- return getLernaSubPackages(lernaPath, options);
53
- }
54
- return getPackageConfigs(process.cwd(), options);
55
- }
56
- if (!options.input.includes('.') || !fs.existsSync(options.input)) {
57
- return getPackageConfigs(process.cwd(), options, options.input);
58
- }
59
- const creator = new ConfigCreator(options);
60
- return creator.getConfig();
61
- }
62
-
63
- export async function bundle(...options) {
64
- const configs = getConfigs({
65
- input: options.filter(x => !x.startsWith('-'))[0],
66
- project: options.includes('-b'),
67
- minify: options.includes('--prod'),
68
- devServer: options.includes('--run'),
69
- stats: options.includes('--stats'),
70
- });
71
- if (!options.includes('--watch')) {
72
- for (let config of configs) {
73
- console.log(`1. ${config.input} -> ${config.output.file}`);
74
- const build = await rollup(config);
75
- await build.write(config.output);
76
- console.log('SUCCESS');
77
- }
78
- return;
79
- }
80
- const watcher = watch(configs);
81
- watcher.on('event', (event) => {
82
- switch (event.code) {
83
- case 'START':
84
- console.clear();
85
- console.log('START BUNDLING');
86
- break;
87
- case 'END':
88
- console.log('FINISH');
89
- break;
90
- case 'BUNDLE_START':
91
- console.log(`1. ${event.input} -> ${event.output}`);
92
- break;
93
- case 'BUNDLE_END':
94
- console.log(`1. ${event.input} -> ${event.output}, (${event.duration / 1000}s)`);
95
- break;
96
-
97
- case 'ERROR':
98
- switch (event.error.code) {
99
- case 'PARSE_ERROR':
100
- console.warn('Error parsing files:');
101
- console.log(`\t${event.error.parserError.message}`);
102
- console.log(`\tat: ${event.error.id}`);
103
- console.log(`\tline: ${event.error.frame}`);
104
- break;
105
- case 'UNRESOLVED_IMPORT':
106
- console.error(event.error.message);
107
- break;
108
- default:
109
- console.warn('Unknown error:', event.error.code);
110
- console.error(event.error);
111
- break;
112
- }
113
- break;
114
- }
115
- });
116
- }
1
+ import {ConfigCreator} from "./rollup.config.js";
2
+ import {rollup, watch} from "rollup";
3
+ import fs from "fs";
4
+ import path from "path";
5
+ import fg from "fast-glob";
6
+
7
+ function getProjectConfig(rootDir, cmmn, options) {
8
+ const configCreator = new ConfigCreator({
9
+ ...options,
10
+ ...cmmn,
11
+ });
12
+ configCreator.setRootDir(rootDir);
13
+ return configCreator.getConfig();
14
+ }
15
+
16
+ function getPackageConfigs(rootDir, options, name = null) {
17
+ const results = [];
18
+ const pkg = JSON.parse(fs.readFileSync(path.join(rootDir, 'package.json')));
19
+ if (name) {
20
+ results.push(...getProjectConfig(rootDir, pkg.cmmn[name], {
21
+ ...options,
22
+ name
23
+ }));
24
+ } else {
25
+ for (let name in pkg.cmmn) {
26
+ results.push(...getProjectConfig(rootDir, pkg.cmmn[name], {
27
+ ...options,
28
+ name
29
+ }));
30
+ }
31
+ }
32
+ return results;
33
+ }
34
+
35
+ function getLernaSubPackages(lernaFile, options) {
36
+ const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
37
+ const packages = config.packages;
38
+ const dirs = packages.flatMap(pkg => fg.sync([pkg], {
39
+ absolute: true,
40
+ globstar: true,
41
+ onlyDirectories: true,
42
+ cwd: path.dirname(lernaFile)
43
+ }));
44
+ return dirs.flatMap(dir => getPackageConfigs(dir, options));
45
+ }
46
+
47
+ function getConfigs(options) {
48
+ if (!options.input || options.project) {
49
+ const rootDir = process.cwd();
50
+ const lernaPath = path.join(rootDir, 'lerna.json');
51
+ if (fs.existsSync(lernaPath)) {
52
+ return getLernaSubPackages(lernaPath, options);
53
+ }
54
+ return getPackageConfigs(process.cwd(), options);
55
+ }
56
+ if (!options.input.includes('.') || !fs.existsSync(options.input)) {
57
+ return getPackageConfigs(process.cwd(), options, options.input);
58
+ }
59
+ const creator = new ConfigCreator(options);
60
+ return creator.getConfig();
61
+ }
62
+
63
+ export async function bundle(...options) {
64
+ const configs = getConfigs({
65
+ input: options.filter(x => !x.startsWith('-'))[0],
66
+ project: options.includes('-b'),
67
+ minify: options.includes('--prod'),
68
+ devServer: options.includes('--run'),
69
+ stats: options.includes('--stats'),
70
+ });
71
+ if (!options.includes('--watch')) {
72
+ for (let config of configs) {
73
+ console.log(`1. ${config.input} -> ${config.output.file}`);
74
+ const build = await rollup(config);
75
+ await build.write(config.output);
76
+ console.log('SUCCESS');
77
+ }
78
+ return;
79
+ }
80
+ const watcher = watch(configs);
81
+ watcher.on('event', (event) => {
82
+ switch (event.code) {
83
+ case 'START':
84
+ console.clear();
85
+ console.log('START BUNDLING');
86
+ break;
87
+ case 'END':
88
+ console.log('FINISH');
89
+ break;
90
+ case 'BUNDLE_START':
91
+ console.log(`1. ${event.input} -> ${event.output}`);
92
+ break;
93
+ case 'BUNDLE_END':
94
+ console.log(`1. ${event.input} -> ${event.output}, (${event.duration / 1000}s)`);
95
+ break;
96
+
97
+ case 'ERROR':
98
+ switch (event.error.code) {
99
+ case 'PARSE_ERROR':
100
+ console.warn('Error parsing files:');
101
+ console.log(`\t${event.error.parserError.message}`);
102
+ console.log(`\tat: ${event.error.id}`);
103
+ console.log(`\tline: ${event.error.frame}`);
104
+ break;
105
+ case 'UNRESOLVED_IMPORT':
106
+ console.error(event.error.message);
107
+ break;
108
+ default:
109
+ console.warn('Unknown error:', event.error.code);
110
+ console.error(event.error);
111
+ break;
112
+ }
113
+ break;
114
+ }
115
+ });
116
+ }