fabrique 0.0.1 → 0.2.0-beta-1
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/.editorconfig +7 -0
- package/.nvmrc +1 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README.md +41 -0
- package/package.json +24 -9
- package/src/actions/create.js +123 -0
- package/src/actions/refactor.js +166 -0
- package/src/actions/upgrade.js +157 -0
- package/src/actions/verdaccio.js +20 -0
- package/src/helpers/cmd.js +45 -0
- package/src/helpers/fs/exists.js +10 -0
- package/src/helpers/fs/explore.js +18 -0
- package/src/helpers/fs/read-json-file.js +11 -0
- package/src/helpers/fs/write-file-safe.js +13 -0
- package/src/helpers/fs/write-json-file.js +12 -0
- package/src/helpers/handle-error.js +14 -0
- package/src/helpers/list-available-templates.js +11 -0
- package/src/helpers/log/log-error.js +7 -0
- package/src/helpers/log/log.js +28 -0
- package/src/helpers/paths.constant.js +15 -0
- package/src/helpers/replace-pattern.js +16 -0
- package/src/index.js +43 -0
- package/templates/lib/.editorconfig +7 -0
- package/templates/lib/.nvmrc +1 -0
- package/templates/lib/.prettierrc +7 -0
- package/templates/lib/.yarnrc.yml +6 -0
- package/templates/lib/CONTRIBUTING.md +40 -0
- package/templates/lib/LICENSE +21 -0
- package/templates/lib/README.md +15 -0
- package/templates/lib/fabrique/actions/build.js +355 -0
- package/templates/lib/fabrique/helpers/cmd.js +44 -0
- package/templates/lib/fabrique/test/tools.ts +18 -0
- package/templates/lib/package.json +30 -0
- package/templates/lib/src/add.test.ts +8 -0
- package/templates/lib/src/add.ts +3 -0
- package/templates/lib/src/style.private.scss +5 -0
- package/templates/lib/src/style.scss +5 -0
- package/templates/lib/src/sub.private.ts +3 -0
- package/templates/lib/src/variables.scss +1 -0
- package/templates/lib/tsconfig.build.json +17 -0
- package/templates/lib/tsconfig.json +37 -0
- package/templates/lib/vitest.config.ts +7 -0
- package/tsconfig.json +43 -0
- package/index.js +0 -13
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} path
|
|
6
|
+
* @param {string|NodeJS.ArrayBufferView} content
|
|
7
|
+
* @param {any?} options
|
|
8
|
+
* @return {Promise<void>}
|
|
9
|
+
*/
|
|
10
|
+
export async function writeFileSafe(path, content, options) {
|
|
11
|
+
await mkdir(dirname(path), { recursive: true });
|
|
12
|
+
await writeFile(path, content, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { writeFileSafe } from './write-file-safe.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Write a json file.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} path
|
|
7
|
+
* @param {json} any
|
|
8
|
+
* @return {Promise<void>}
|
|
9
|
+
*/
|
|
10
|
+
export function writeJsonFile(path, json) {
|
|
11
|
+
return writeFileSafe(path, JSON.stringify(json, null, 2));
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { logError } from './log/log-error.js';
|
|
2
|
+
|
|
3
|
+
export function handleError(callback) {
|
|
4
|
+
return (...args) => {
|
|
5
|
+
try {
|
|
6
|
+
const result = callback(...args);
|
|
7
|
+
if (result instanceof Promise) {
|
|
8
|
+
return result.catch(logError);
|
|
9
|
+
}
|
|
10
|
+
} catch (error) {
|
|
11
|
+
logError(error);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { TEMPLATES_PATH } from './paths.constant.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* List the available templates.
|
|
6
|
+
*
|
|
7
|
+
* @return {Promise<string[]>}
|
|
8
|
+
*/
|
|
9
|
+
export function listAvailableTemplates() {
|
|
10
|
+
return readdir(TEMPLATES_PATH);
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {'none' | 'error' | 'warn' | 'success'} type
|
|
5
|
+
* @param {string} message
|
|
6
|
+
*/
|
|
7
|
+
export function log(type, message) {
|
|
8
|
+
if (type === 'none') {
|
|
9
|
+
console.log(message);
|
|
10
|
+
} else {
|
|
11
|
+
let color;
|
|
12
|
+
switch (type) {
|
|
13
|
+
case 'warn':
|
|
14
|
+
color = chalk.yellowBright;
|
|
15
|
+
break;
|
|
16
|
+
case 'error':
|
|
17
|
+
color = chalk.redBright;
|
|
18
|
+
break;
|
|
19
|
+
case 'success':
|
|
20
|
+
color = chalk.greenBright;
|
|
21
|
+
break;
|
|
22
|
+
default:
|
|
23
|
+
throw new Error('Invalid type');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
console.log(color(`${chalk.bold(`[${type.toUpperCase()}]:`)} ${message}`));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
|
|
4
|
+
export const ROOT_PATH = join(dirname(fileURLToPath(import.meta.url)), '../..');
|
|
5
|
+
export const TEMPLATES_PATH = join(ROOT_PATH, 'templates');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns the template path from its name.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} name
|
|
11
|
+
* @return {string}
|
|
12
|
+
*/
|
|
13
|
+
export function getTemplatePath(name) {
|
|
14
|
+
return join(TEMPLATES_PATH, name);
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replaces some patterns (`{{name}}`) into content.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} content
|
|
5
|
+
* @param {Record<string, string>} values
|
|
6
|
+
* @return {string}
|
|
7
|
+
*/
|
|
8
|
+
export function replacePattern(content, values) {
|
|
9
|
+
return content.replace(/\{\{(\w+)\}\}/g, (_, pattern) => {
|
|
10
|
+
if (Object.hasOwn(values, pattern)) {
|
|
11
|
+
return values[pattern];
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error(`Unknown pattern ${JSON.stringify(pattern)}.`);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { program } from 'commander';
|
|
4
|
+
import { create } from './actions/create.js';
|
|
5
|
+
import { refactor } from './actions/refactor.js';
|
|
6
|
+
import { upgrade } from './actions/upgrade.js';
|
|
7
|
+
import { verdaccio } from './actions/verdaccio.js';
|
|
8
|
+
import { handleError } from './helpers/handle-error.js';
|
|
9
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
10
|
+
|
|
11
|
+
// tuto: https://medium.com/nmc-techblog/building-a-cli-with-node-js-in-2024-c278802a3ef5
|
|
12
|
+
|
|
13
|
+
program.name('fabrique').description('CLI to build typescript libraries.').version(pkg.version);
|
|
14
|
+
|
|
15
|
+
// CMD: create <type> <name>
|
|
16
|
+
program
|
|
17
|
+
.command('create')
|
|
18
|
+
.description('create a new library')
|
|
19
|
+
.argument('<type>', 'type')
|
|
20
|
+
.argument('<name>', 'name')
|
|
21
|
+
.action(handleError(create));
|
|
22
|
+
|
|
23
|
+
// CMD: upgrade
|
|
24
|
+
program
|
|
25
|
+
.command('upgrade')
|
|
26
|
+
.description('upgrade an existing library')
|
|
27
|
+
.option('--force', 'forces the update', false)
|
|
28
|
+
.action(handleError(upgrade));
|
|
29
|
+
|
|
30
|
+
// CMD: refactor <from> <to>
|
|
31
|
+
program
|
|
32
|
+
.command('refactor')
|
|
33
|
+
.description('refactor recursively many files')
|
|
34
|
+
.argument('<from>', 'from')
|
|
35
|
+
.argument('<to>', 'to')
|
|
36
|
+
.option('--dry', 'runs without modifying the files', false)
|
|
37
|
+
.option('--cwd', 'common working directory', process.cwd)
|
|
38
|
+
.action(handleError(refactor));
|
|
39
|
+
|
|
40
|
+
// CMD: verdaccio
|
|
41
|
+
program.command('verdaccio').description('lanches verdaccio').action(handleError(verdaccio));
|
|
42
|
+
|
|
43
|
+
program.parse();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v22
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
This projet is open to everyone. Feel free to test the library, share it, improve it, and create merge requests.
|
|
4
|
+
|
|
5
|
+
### Getting started
|
|
6
|
+
|
|
7
|
+
The library requires [Node.js](https://nodejs.org/en) `22+` and `yarn`.
|
|
8
|
+
|
|
9
|
+
First, we have to switch to the correct version of node:
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
nvm use
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
If you don't have `nvm`, you may manually install and use Node.js 22+.
|
|
16
|
+
|
|
17
|
+
Then, we have to use the proper package manager (here `yarn`):
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
corepack enable
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
And install the dependencies:
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
yarn install
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Code
|
|
30
|
+
|
|
31
|
+
- The source code is located in the `src` directory.
|
|
32
|
+
- The projet uses [prettier](https://prettier.io/) to format the code. You'll want to enable and configure it in your IDE.
|
|
33
|
+
- The tests are run with [vitest](https://vitest.dev)
|
|
34
|
+
|
|
35
|
+
### Commands
|
|
36
|
+
|
|
37
|
+
- `fb:build`: builds the library.
|
|
38
|
+
- `fb:format`: formats the code using `prettier`.
|
|
39
|
+
- `fb:test`: runs the tests using `vitest`.
|
|
40
|
+
- `fb:dev`: builds the lib in `dev` mode, and publishes it on a local `verdaccio`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 {{author}}
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/{{libname}})
|
|
2
|
+

|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
## {{libname}}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
yarn add {{libname}}
|
|
13
|
+
# or
|
|
14
|
+
npm install {{libname}} --save
|
|
15
|
+
```
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { cp, glob, readFile, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { isAbsolute, join } from 'node:path';
|
|
3
|
+
import { cmd } from '../helpers/cmd.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds the lib.
|
|
7
|
+
* @param {{ cwd?: string; dev?: boolean }} options
|
|
8
|
+
* @return {Promise<void>}
|
|
9
|
+
*/
|
|
10
|
+
async function build({ dev = false } = {}) {
|
|
11
|
+
const rootPath = '.';
|
|
12
|
+
const sourcePath = './src';
|
|
13
|
+
const destinationPath = './dist';
|
|
14
|
+
|
|
15
|
+
await removeDestination(destinationPath);
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const [withProtected] = await Promise.all([
|
|
19
|
+
buildTypescript(sourcePath),
|
|
20
|
+
buildScss(sourcePath, destinationPath),
|
|
21
|
+
copyOtherFiles(rootPath, destinationPath),
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
await buildPackageJsonFile(destinationPath, {
|
|
25
|
+
dev,
|
|
26
|
+
withProtected,
|
|
27
|
+
});
|
|
28
|
+
} catch (error) {
|
|
29
|
+
await removeDestination(destinationPath);
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
console.log('Library built with success !');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Removes the destination folder.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} destinationPath
|
|
40
|
+
* @return {Promise<void>}
|
|
41
|
+
*/
|
|
42
|
+
async function removeDestination(destinationPath) {
|
|
43
|
+
await rm(destinationPath, { recursive: true, force: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Builds the typescript part.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} sourcePath
|
|
50
|
+
* @return {Promise<boolean>}
|
|
51
|
+
*/
|
|
52
|
+
async function buildTypescript(sourcePath) {
|
|
53
|
+
const typescriptIndexFilePath = await buildTypescriptIndexFile(sourcePath);
|
|
54
|
+
const typescriptProtectedIndexFilePath = await buildTypescriptProtectedIndexFile(sourcePath);
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
await compileTypescript();
|
|
58
|
+
// await copyTypescriptFiles(sourcePath, destinationPath);
|
|
59
|
+
} finally {
|
|
60
|
+
await removeTypescriptIndexFile(typescriptIndexFilePath);
|
|
61
|
+
if (typescriptProtectedIndexFilePath !== null) {
|
|
62
|
+
await removeTypescriptIndexFile(typescriptProtectedIndexFilePath);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return typescriptProtectedIndexFilePath !== null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Builds the scss part.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} sourcePath
|
|
73
|
+
* @param {string} destinationPath
|
|
74
|
+
* @return {Promise<void>}
|
|
75
|
+
*/
|
|
76
|
+
async function buildScss(sourcePath, destinationPath) {
|
|
77
|
+
await copyScssFiles(sourcePath, destinationPath);
|
|
78
|
+
await buildScssIndexFile(destinationPath);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Builds the typescript index file used to export all public APIs.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} cwd
|
|
85
|
+
* @return {Promise<string>}
|
|
86
|
+
*/
|
|
87
|
+
async function buildTypescriptIndexFile(cwd = process.cwd()) {
|
|
88
|
+
console.log('Building typescript index file...');
|
|
89
|
+
|
|
90
|
+
const content = (
|
|
91
|
+
await Array.fromAsync(
|
|
92
|
+
glob('./**/*.ts', {
|
|
93
|
+
cwd,
|
|
94
|
+
exclude: (path) => {
|
|
95
|
+
return (
|
|
96
|
+
path.endsWith('.spec.ts') ||
|
|
97
|
+
path.endsWith('.test.ts') ||
|
|
98
|
+
path.includes('.private') ||
|
|
99
|
+
path.includes('.protected')
|
|
100
|
+
);
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
.map((path) => {
|
|
106
|
+
return `export * from './${path.slice(0, -3)}.js';`;
|
|
107
|
+
})
|
|
108
|
+
.join('\n');
|
|
109
|
+
|
|
110
|
+
if (content === '') {
|
|
111
|
+
throw new Error('Nothing exported.');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const indexFilePath = join(cwd, 'index.ts');
|
|
115
|
+
await writeFile(indexFilePath, content + '\n');
|
|
116
|
+
|
|
117
|
+
return indexFilePath;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Builds the typescript index file used to export all protected APIs.
|
|
122
|
+
*
|
|
123
|
+
* @param {string} cwd
|
|
124
|
+
* @return {Promise<string | null>}
|
|
125
|
+
*/
|
|
126
|
+
async function buildTypescriptProtectedIndexFile(cwd = process.cwd()) {
|
|
127
|
+
console.log('Building typescript protected index file...');
|
|
128
|
+
|
|
129
|
+
const content = (
|
|
130
|
+
await Array.fromAsync(
|
|
131
|
+
glob('{./**/*.protected/**/*.ts,./**/*.protected.ts,}', {
|
|
132
|
+
cwd,
|
|
133
|
+
exclude: (path) => {
|
|
134
|
+
return (
|
|
135
|
+
path.endsWith('.spec.ts') || path.endsWith('.test.ts') || path.includes('.private')
|
|
136
|
+
);
|
|
137
|
+
},
|
|
138
|
+
}),
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
.map((path) => {
|
|
142
|
+
return `export * from './${path.slice(0, -3)}.js';`;
|
|
143
|
+
})
|
|
144
|
+
.join('\n');
|
|
145
|
+
|
|
146
|
+
if (content === '') {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const indexFilePath = join(cwd, 'index.protected.ts');
|
|
151
|
+
await writeFile(indexFilePath, content + '\n');
|
|
152
|
+
|
|
153
|
+
return indexFilePath;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Compiles the typescript files.
|
|
158
|
+
*
|
|
159
|
+
* @param {string | undefined } cwd
|
|
160
|
+
* @return {Promise<void>}
|
|
161
|
+
*/
|
|
162
|
+
async function compileTypescript(cwd = process.cwd()) {
|
|
163
|
+
console.log('Compiling typescript...');
|
|
164
|
+
|
|
165
|
+
await cmd('tsc', ['-p', './tsconfig.build.json'], { cwd });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Copies typescript files into the destination.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} sourcePath
|
|
172
|
+
* @param {string} destinationPath
|
|
173
|
+
* @return {Promise<void>}
|
|
174
|
+
*/
|
|
175
|
+
async function copyTypescriptFiles(sourcePath, destinationPath) {
|
|
176
|
+
console.log('Copying typescript files...');
|
|
177
|
+
|
|
178
|
+
for await (const path of glob('./**/!(*.spec|*.test).ts', { cwd: sourcePath })) {
|
|
179
|
+
await cp(join(sourcePath, path), join(destinationPath, path));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Builds the typescript index file used to export all public APIs.
|
|
185
|
+
*
|
|
186
|
+
* @param {string} cwd
|
|
187
|
+
* @return {Promise<void>}
|
|
188
|
+
*/
|
|
189
|
+
async function buildScssIndexFile(cwd = process.cwd()) {
|
|
190
|
+
console.log('Building scss index file...');
|
|
191
|
+
|
|
192
|
+
// TODO exclude directories too
|
|
193
|
+
const content = (await Array.fromAsync(glob('./**/!(*.private).scss', { cwd })))
|
|
194
|
+
.map((path) => {
|
|
195
|
+
return `@forward './${path.slice(0, -5)}';`;
|
|
196
|
+
})
|
|
197
|
+
.join('\n');
|
|
198
|
+
|
|
199
|
+
if (content === '') {
|
|
200
|
+
console.log('=> No scss file to export.');
|
|
201
|
+
} else {
|
|
202
|
+
const indexFilePath = join(cwd, 'index.scss');
|
|
203
|
+
await writeFile(indexFilePath, content + '\n');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Copies scss files into the destination.
|
|
209
|
+
*
|
|
210
|
+
* @param {string} sourcePath
|
|
211
|
+
* @param {string} destinationPath
|
|
212
|
+
* @return {Promise<void>}
|
|
213
|
+
*/
|
|
214
|
+
async function copyScssFiles(sourcePath, destinationPath) {
|
|
215
|
+
console.log('Copying scss files...');
|
|
216
|
+
|
|
217
|
+
for await (const path of glob('./**/!(*.private).scss', { cwd: sourcePath })) {
|
|
218
|
+
// await cp(join(sourcePath, path), join(destinationPath, path));
|
|
219
|
+
await writeFile(
|
|
220
|
+
join(destinationPath, path),
|
|
221
|
+
fixScssFileContent(await readFile(join(sourcePath, path), { encoding: 'utf8' })),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Fixes the content of a scss file.
|
|
228
|
+
*
|
|
229
|
+
* @param {string} content
|
|
230
|
+
* @return {string}
|
|
231
|
+
*/
|
|
232
|
+
function fixScssFileContent(content) {
|
|
233
|
+
return content.replace(/@(use|import)\s+['"]([^'"]*)['"]/g, (_, type, importPath) => {
|
|
234
|
+
if (isAbsolute(importPath)) {
|
|
235
|
+
throw new Error(`Import path ${importPath} cannot be absolute.`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (!importPath.startsWith('./') && !importPath.startsWith('../')) {
|
|
239
|
+
importPath = `./${importPath}`;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (importPath.endsWith('.scss')) {
|
|
243
|
+
importPath = importPath.slice(0, -5);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return `@${type} '${fixScssFilePath(importPath)}'`;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Fixes a scss file path.
|
|
252
|
+
*
|
|
253
|
+
* @param {string} path
|
|
254
|
+
* @return {string}
|
|
255
|
+
*/
|
|
256
|
+
function fixScssFilePath(path) {
|
|
257
|
+
if (!path.startsWith('@')) {
|
|
258
|
+
if (isAbsolute(path)) {
|
|
259
|
+
throw new Error(`Import path ${path} cannot be absolute.`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!path.startsWith('./') && !path.startsWith('../')) {
|
|
263
|
+
path = `./${path}`;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (path.endsWith('.scss')) {
|
|
268
|
+
path = path.slice(0, -5);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return path;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Copies other files.
|
|
276
|
+
*
|
|
277
|
+
* @param {string} rootPath
|
|
278
|
+
* @param {string} destinationPath
|
|
279
|
+
* @return {Promise<void>}
|
|
280
|
+
*/
|
|
281
|
+
async function copyOtherFiles(rootPath, destinationPath) {
|
|
282
|
+
console.log('Copying other files...');
|
|
283
|
+
|
|
284
|
+
await Promise.all(
|
|
285
|
+
['README.md', 'CONTRIBUTING.md', 'LICENSE'].map((path) => {
|
|
286
|
+
return cp(join(rootPath, path), join(destinationPath, path)).catch(() => {
|
|
287
|
+
console.log(`Missing file: ${path}`);
|
|
288
|
+
});
|
|
289
|
+
}),
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Removes the index file.
|
|
295
|
+
*
|
|
296
|
+
* @param {string} indexFilePath
|
|
297
|
+
* @return {Promise<void>}
|
|
298
|
+
*/
|
|
299
|
+
async function removeTypescriptIndexFile(indexFilePath) {
|
|
300
|
+
await rm(indexFilePath);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Generates the package.json to publish.
|
|
305
|
+
*
|
|
306
|
+
* @param {string} destinationPath
|
|
307
|
+
* @param {{ cwd?: string; dev?: boolean, withProtected?: boolean }} options
|
|
308
|
+
* @return {Promise<void>}
|
|
309
|
+
*/
|
|
310
|
+
async function buildPackageJsonFile(
|
|
311
|
+
destinationPath,
|
|
312
|
+
{ cwd = process.cwd(), dev = false, withProtected = false } = {},
|
|
313
|
+
) {
|
|
314
|
+
console.log('Building package.json...');
|
|
315
|
+
|
|
316
|
+
const fileName = 'package.json';
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @type any
|
|
320
|
+
*/
|
|
321
|
+
const pkg = JSON.parse(await readFile(join(cwd, fileName), { encoding: 'utf8' }));
|
|
322
|
+
|
|
323
|
+
const indexTypesPath = './index.d.ts';
|
|
324
|
+
|
|
325
|
+
if (dev) {
|
|
326
|
+
pkg.version += `-dev.${Date.now()}`;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
Object.assign(pkg, {
|
|
330
|
+
exports: {
|
|
331
|
+
'.': {
|
|
332
|
+
types: indexTypesPath,
|
|
333
|
+
default: './index.js',
|
|
334
|
+
},
|
|
335
|
+
...(withProtected
|
|
336
|
+
? {
|
|
337
|
+
'./protected': {
|
|
338
|
+
types: './index.protected.d.ts',
|
|
339
|
+
default: './index.protected.js',
|
|
340
|
+
},
|
|
341
|
+
}
|
|
342
|
+
: {}),
|
|
343
|
+
},
|
|
344
|
+
typings: indexTypesPath,
|
|
345
|
+
types: indexTypesPath,
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
await writeFile(join(destinationPath, fileName), JSON.stringify(pkg, null, 2));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/*-----------------------------------*/
|
|
352
|
+
|
|
353
|
+
const [dev = false] = process.argv.slice(2);
|
|
354
|
+
|
|
355
|
+
build({ dev });
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs "command" into the terminal.
|
|
5
|
+
* @param {string} command
|
|
6
|
+
* @param {readonly string[]} args
|
|
7
|
+
* @param {SpawnOptions | undefined} options
|
|
8
|
+
*/
|
|
9
|
+
export async function cmd(command, args = [], options) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
console.log(`> ${command} ${args.join(' ')}`);
|
|
12
|
+
|
|
13
|
+
const childProcess = spawn(command, args, {
|
|
14
|
+
cwd: process.cwd(),
|
|
15
|
+
detached: false,
|
|
16
|
+
stdio: 'inherit',
|
|
17
|
+
...options,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
childProcess.on('exit', (code) => {
|
|
21
|
+
if (code === 0) {
|
|
22
|
+
resolve();
|
|
23
|
+
} else {
|
|
24
|
+
reject(new Error(`Exit with code ${code}.`));
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
childProcess.on('error', (err) => {
|
|
29
|
+
reject(err);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// process.on('SIGINT', () => {
|
|
33
|
+
// childProcess.kill('SIGINT');
|
|
34
|
+
// });
|
|
35
|
+
//
|
|
36
|
+
// process.on('SIGTERM', () => {
|
|
37
|
+
// childProcess.kill('SIGTERM');
|
|
38
|
+
// });
|
|
39
|
+
//
|
|
40
|
+
// process.on('SIGBREAK', () => {
|
|
41
|
+
// childProcess.kill('SIGBREAK');
|
|
42
|
+
// });
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export namespace testTools {
|
|
2
|
+
export function sleep(t: number): Promise<void> {
|
|
3
|
+
return new Promise((_) => setTimeout(_, t));
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function polyfillRequestIdleCallback(): void {
|
|
7
|
+
globalThis.requestIdleCallback ??= (
|
|
8
|
+
callback: IdleRequestCallback,
|
|
9
|
+
{ timeout = 0 }: IdleRequestOptions = {},
|
|
10
|
+
): number => {
|
|
11
|
+
return setTimeout(callback, timeout);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
globalThis.cancelIdleCallback ??= (handle: number): void => {
|
|
15
|
+
clearTimeout(handle);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|