@cmmn/tools 1.6.14 → 1.6.18

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
File without changes
@@ -1,62 +1,62 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import fg from "fast-glob";
4
-
5
- function getProjectConfig(rootDir, cmmn, options) {
6
- return {
7
- ...options,
8
- ...cmmn,
9
- rootDir: rootDir
10
- };
11
- }
12
-
13
- function getPackageConfigs(rootDir, options, name = null) {
14
- const pckPath = path.join(rootDir, 'package.json');
15
- if (!fs.existsSync(pckPath))
16
- return [];
17
- const results = [];
18
- const pkg = JSON.parse(fs.readFileSync(pckPath));
19
- if (name) {
20
- results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
21
- ...options,
22
- name,
23
- package: pkg.name,
24
- }));
25
- } else {
26
- for (let name in pkg.cmmn) {
27
- results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
28
- ...options,
29
- name,
30
- package: pkg.name,
31
- }));
32
- }
33
- }
34
- return results;
35
- }
36
-
37
- function getLernaSubPackages(lernaFile, options) {
38
- const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
39
- const packages = config.packages;
40
- const dirs = packages.flatMap(pkg => fg.sync([pkg], {
41
- absolute: true,
42
- globstar: true,
43
- onlyDirectories: true,
44
- cwd: path.dirname(lernaFile)
45
- }));
46
- return dirs.flatMap(dir => getPackageConfigs(dir, options));
47
- }
48
-
49
- export function getConfigOptions(options) {
50
- if (!options.input || options.project) {
51
- const rootDir = process.cwd();
52
- const lernaPath = path.join(rootDir, 'lerna.json');
53
- if (fs.existsSync(lernaPath)) {
54
- return getLernaSubPackages(lernaPath, options);
55
- }
56
- return getPackageConfigs(process.cwd(), options);
57
- }
58
- if (!options.input.includes('.') || !fs.existsSync(options.input)) {
59
- return getPackageConfigs(process.cwd(), options, options.input);
60
- }
61
- return [options];
62
- }
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import fg from "fast-glob";
4
+
5
+ function getProjectConfig(rootDir, cmmn, options) {
6
+ return {
7
+ ...options,
8
+ ...cmmn,
9
+ rootDir: rootDir
10
+ };
11
+ }
12
+
13
+ function getPackageConfigs(rootDir, options, name = null) {
14
+ const pckPath = path.join(rootDir, 'package.json');
15
+ if (!fs.existsSync(pckPath))
16
+ return [];
17
+ const results = [];
18
+ const pkg = JSON.parse(fs.readFileSync(pckPath));
19
+ if (name) {
20
+ results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
21
+ ...options,
22
+ name,
23
+ package: pkg.name,
24
+ }));
25
+ } else {
26
+ for (let name in pkg.cmmn) {
27
+ results.push(getProjectConfig(rootDir, pkg.cmmn[name], {
28
+ ...options,
29
+ name,
30
+ package: pkg.name,
31
+ }));
32
+ }
33
+ }
34
+ return results;
35
+ }
36
+
37
+ function getLernaSubPackages(lernaFile, options) {
38
+ const config = JSON.parse(fs.readFileSync(lernaFile, 'utf8'));
39
+ const packages = config.packages;
40
+ const dirs = packages.flatMap(pkg => fg.sync([pkg], {
41
+ absolute: true,
42
+ globstar: true,
43
+ onlyDirectories: true,
44
+ cwd: path.dirname(lernaFile)
45
+ }));
46
+ return dirs.flatMap(dir => getPackageConfigs(dir, options));
47
+ }
48
+
49
+ export function getConfigOptions(options) {
50
+ if (!options.input || options.project) {
51
+ const rootDir = process.cwd();
52
+ const lernaPath = path.join(rootDir, 'lerna.json');
53
+ if (fs.existsSync(lernaPath)) {
54
+ return getLernaSubPackages(lernaPath, options);
55
+ }
56
+ return getPackageConfigs(process.cwd(), options);
57
+ }
58
+ if (!options.input.includes('.') || !fs.existsSync(options.input)) {
59
+ return getPackageConfigs(process.cwd(), options, options.input);
60
+ }
61
+ return [options];
62
+ }
@@ -0,0 +1,43 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
2
+ import { basename, dirname, join } from 'path';
3
+ import { createFilter } from 'rollup-pluginutils';
4
+ const defaultExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
5
+
6
+ export function images(options = {}) {
7
+ const extensions = options.extensions || defaultExtensions;
8
+ const includes = extensions.map(e => `**/*${e}`);
9
+ const filter = createFilter(options.include || includes, options.exclude);
10
+ const assetsDir = options.output ?? 'assets';
11
+ let images = [];
12
+
13
+ function generateBundle(outputOptions, rendered) {
14
+ if (!images.length)
15
+ return;
16
+ const outDir =
17
+ outputOptions.dir || dirname(outputOptions.dest || outputOptions.file);
18
+ const distDir = join(outDir, assetsDir);
19
+ if (!existsSync(distDir)) {
20
+ mkdirSync(distDir, {recursive: true});
21
+ }
22
+ images.forEach(id => {
23
+ writeFileSync(`${distDir}/${basename(id)}`, readFileSync(id));
24
+ });
25
+ }
26
+
27
+ return {
28
+ name: 'image-file',
29
+ load(id) {
30
+ if ('string' !== typeof id || !filter(id)) {
31
+ return null;
32
+ }
33
+
34
+ if (images.indexOf(id) < 0) {
35
+ images.push(id);
36
+ }
37
+ return `export default '/${assetsDir}/${basename(id)}';`;
38
+ },
39
+ generateBundle,
40
+ ongenerate: generateBundle
41
+ };
42
+ }
43
+