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
package/.editorconfig
ADDED
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v22
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Valentin RICHARD
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/fabrique)
|
|
2
|
+

|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
## Fabrique
|
|
7
|
+
|
|
8
|
+
`Fabrique` is a **cli to create and build libraries**,
|
|
9
|
+
letting the user **focus on the functionalities** of its library, instead of the build process.
|
|
10
|
+
|
|
11
|
+
With `Fabrique` you'll be able to:
|
|
12
|
+
|
|
13
|
+
- create rapidly a library with all the tools and scripts to:
|
|
14
|
+
- build and publish your library
|
|
15
|
+
- write tests for it
|
|
16
|
+
- format your code
|
|
17
|
+
- and debug it
|
|
18
|
+
- upgrade any Fabrique project to get the last functionalities in one command line.
|
|
19
|
+
|
|
20
|
+
`Fabrique` is not intended to provide ci scripts and pipelines to automate the processes.
|
|
21
|
+
Instead, it gives a uniform and simple entry point for everyone, to publish rapidly our own libraries,
|
|
22
|
+
on which we may add automation, scripts, and complexity.
|
|
23
|
+
|
|
24
|
+
### Motivation
|
|
25
|
+
|
|
26
|
+
When we develop javascript/typescript libraries, we frequently create new repositories,
|
|
27
|
+
and maintains the scripts to build, test, and debug them.
|
|
28
|
+
This tends to become rapidly cumbersome, especially when their number grows.
|
|
29
|
+
We may choose to opt in for a _monorepo_, and I'll tell you: yes, in many cases, this is the optimal solution.
|
|
30
|
+
However, in many other cases, one library per-repo makes sense, and this is where `Fabrique` comes in.
|
|
31
|
+
|
|
32
|
+
Another goal is to focus on simplicity: for new incomers in javascript,
|
|
33
|
+
learning all the tools and build processes is complicated.
|
|
34
|
+
You want to write your library, but you have to learn `vite`, `jest`, how to build and publish a lib,
|
|
35
|
+
install `prettier`, etc. With `Fabrique` all comes in pre-configured.
|
|
36
|
+
|
|
37
|
+
## 📦 Installation
|
|
38
|
+
|
|
39
|
+
```shell
|
|
40
|
+
yarn add fabrique
|
|
41
|
+
```
|
package/package.json
CHANGED
|
@@ -1,18 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fabrique",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "https://github.com/lifaon74/fabrique"
|
|
9
|
-
},
|
|
3
|
+
"version": "0.2.0-beta-1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"bin": "./src/index.js",
|
|
10
7
|
"scripts": {
|
|
11
|
-
"
|
|
8
|
+
"run": "node src/index.js",
|
|
9
|
+
"link": "npm link",
|
|
10
|
+
"publish:lib": "npm publish --access public --registry https://registry.npmjs.org",
|
|
11
|
+
"format": "prettier --write --ignore-unknown --config ./.prettierrc \"src/**/*.{js,jsx,ts,tsx,html,css,scss}\""
|
|
12
12
|
},
|
|
13
|
+
"description": "CLI to build a library",
|
|
13
14
|
"keywords": [],
|
|
14
15
|
"author": "Valentin Richard",
|
|
15
16
|
"license": "MIT",
|
|
16
17
|
"access": "public",
|
|
17
|
-
"
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/lifaon74/fabrique"
|
|
21
|
+
},
|
|
22
|
+
"packageManager": "yarn@4.5.1",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@inquirer/prompts": "^7.0.1",
|
|
25
|
+
"prettier": "^3.3.3",
|
|
26
|
+
"prettier-plugin-organize-imports": "^4.1.0",
|
|
27
|
+
"typescript": "^5.6.3"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"chalk": "^5.3.0",
|
|
31
|
+
"commander": "^12.1.0"
|
|
32
|
+
}
|
|
18
33
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { confirm, input } from '@inquirer/prompts';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { cp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { basename, join } from 'node:path';
|
|
5
|
+
import { cmd } from '../helpers/cmd.js';
|
|
6
|
+
import { exists } from '../helpers/fs/exists.js';
|
|
7
|
+
import { explore } from '../helpers/fs/explore.js';
|
|
8
|
+
import { readJsonFile } from '../helpers/fs/read-json-file.js';
|
|
9
|
+
import { writeJsonFile } from '../helpers/fs/write-json-file.js';
|
|
10
|
+
import { log } from '../helpers/log/log.js';
|
|
11
|
+
import { ROOT_PATH } from '../helpers/paths.constant.js';
|
|
12
|
+
import { replacePattern } from '../helpers/replace-pattern.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new lib.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} type
|
|
18
|
+
* @param {string} name
|
|
19
|
+
* @return {Promise<void>}
|
|
20
|
+
*/
|
|
21
|
+
export async function create(type, name) {
|
|
22
|
+
const templates = ['lib'];
|
|
23
|
+
|
|
24
|
+
if (!templates.includes(type)) {
|
|
25
|
+
throw new Error(`type must be one of the following: ${templates.join(', ')}.`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const templatePath = join(ROOT_PATH, `./templates/${type}`);
|
|
29
|
+
const destinationPath = `./${name}`;
|
|
30
|
+
|
|
31
|
+
if (await exists(destinationPath)) {
|
|
32
|
+
log('warn', 'A file or directory with the same name already exists.');
|
|
33
|
+
if (
|
|
34
|
+
await confirm({
|
|
35
|
+
message: 'Remove it ?',
|
|
36
|
+
})
|
|
37
|
+
) {
|
|
38
|
+
await rm(destinationPath, { recursive: true });
|
|
39
|
+
log('success', 'Removed');
|
|
40
|
+
} else {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const values = {
|
|
46
|
+
libname: await input({ message: 'Library name', default: name, required: true }),
|
|
47
|
+
description: await input({ message: 'Description' }),
|
|
48
|
+
author: await input({ message: 'Author' }),
|
|
49
|
+
giturl: await input({ message: 'Git url' }),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
console.log(chalk.magentaBright('Initializing the library...'));
|
|
53
|
+
|
|
54
|
+
await cp(templatePath, destinationPath, { recursive: true });
|
|
55
|
+
|
|
56
|
+
await replacePatterns(destinationPath, values);
|
|
57
|
+
|
|
58
|
+
const version = (await readJsonFile(join(ROOT_PATH, 'package.json'))).version;
|
|
59
|
+
await upgradePackage(destinationPath, {
|
|
60
|
+
version,
|
|
61
|
+
type,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
await cmd('yarn', [], {
|
|
65
|
+
cwd: destinationPath,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
log('success', `Library ${JSON.stringify(name)} created.`);
|
|
69
|
+
console.log(chalk.magentaBright('Go to your project with:'), `cd ${destinationPath}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/*---*/
|
|
73
|
+
/**
|
|
74
|
+
* Replaces the patterns `{{name}}` from all the files found inside `cwd`.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} ext
|
|
77
|
+
* @return {RegExp}
|
|
78
|
+
*/
|
|
79
|
+
function p_ext(ext) {
|
|
80
|
+
return new RegExp(`^.*\.${ext}$`, 'i');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function p_name(name) {
|
|
84
|
+
return new RegExp(`^${name}$`, 'i');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const DEFAULT_REPLACE_PATTERNS = [p_ext('md'), p_name('LICENSE'), p_name('package.json')];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Replaces the patterns `{{name}}` from all the files found inside `cwd`.
|
|
91
|
+
*
|
|
92
|
+
* @param {string} cwd
|
|
93
|
+
* @param {Record<string, string>} values
|
|
94
|
+
* @param {readonly RegExp[]=} patterns
|
|
95
|
+
* @return {Promise<void>}
|
|
96
|
+
*/
|
|
97
|
+
async function replacePatterns(cwd, values, patterns = DEFAULT_REPLACE_PATTERNS) {
|
|
98
|
+
for await (const [path, stats] of explore(cwd)) {
|
|
99
|
+
if (stats.isFile()) {
|
|
100
|
+
const name = basename(path);
|
|
101
|
+
if (patterns.some((pattern) => pattern.test(name))) {
|
|
102
|
+
await writeFile(path, replacePattern(await readFile(path, { encoding: 'utf8' }), values));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Upgrades the destination `package.json`.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} destinationPath
|
|
112
|
+
* @param {{version: string; type: string;}} fabriqueConfig
|
|
113
|
+
* @return {Promise<void>}
|
|
114
|
+
*/
|
|
115
|
+
async function upgradePackage(destinationPath, fabriqueConfig) {
|
|
116
|
+
const packageJsonPath = join(destinationPath, 'package.json');
|
|
117
|
+
const packageJson = await readJsonFile(packageJsonPath);
|
|
118
|
+
const newPackageJson = {
|
|
119
|
+
...packageJson,
|
|
120
|
+
fabrique: fabriqueConfig,
|
|
121
|
+
};
|
|
122
|
+
await writeJsonFile(packageJsonPath, newPackageJson);
|
|
123
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { readFile, rename, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { explore } from '../helpers/fs/explore.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Refactor recursively many files
|
|
8
|
+
*
|
|
9
|
+
* @param {string} cwd
|
|
10
|
+
* @param {string} from
|
|
11
|
+
* @param {string} to
|
|
12
|
+
* @param {{dry?: boolean, cwd?: string }} options
|
|
13
|
+
* @return {Promise<void>}
|
|
14
|
+
*/
|
|
15
|
+
export async function refactor(from, to, { dry = false, cwd = process.cwd() }) {
|
|
16
|
+
const refactor = createRefactorFunction(from, to);
|
|
17
|
+
|
|
18
|
+
for await (const [path, stats] of explore(cwd)) {
|
|
19
|
+
if (stats.isFile()) {
|
|
20
|
+
await refactorFileContent(path, refactor, dry);
|
|
21
|
+
await refactorFileName(path, refactor, dry);
|
|
22
|
+
} else if (stats.isDirectory()) {
|
|
23
|
+
await refactorFileName(path, refactor, dry);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*----------*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} input
|
|
32
|
+
* @return {string}
|
|
33
|
+
*/
|
|
34
|
+
function dashCaseToCamelCase(input) {
|
|
35
|
+
return input.replace(/-([a-z])/g, (_, firstLetter) => {
|
|
36
|
+
return firstLetter.toUpperCase();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} input
|
|
42
|
+
* @return {string}
|
|
43
|
+
*/
|
|
44
|
+
function dashCaseToPascalCase(input) {
|
|
45
|
+
const output = dashCaseToCamelCase(input);
|
|
46
|
+
return `${output.slice(0, 1).toUpperCase()}${output.slice(1)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} input
|
|
51
|
+
* @return {string}
|
|
52
|
+
*/
|
|
53
|
+
function dashCaseToUpperCase(input) {
|
|
54
|
+
return input.replace(/-/g, '_').toUpperCase();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {string} from
|
|
59
|
+
* @param {string} to
|
|
60
|
+
* @return {(input: string) => string}
|
|
61
|
+
*/
|
|
62
|
+
function createRefactorFunction(from, to) {
|
|
63
|
+
if (!from.includes('-') && to.includes('-')) {
|
|
64
|
+
throw new Error("'from' must be dash-case");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* DASH CASE */
|
|
68
|
+
const REPLACE_DASH_CASE_REGEXP = new RegExp(from, 'g');
|
|
69
|
+
|
|
70
|
+
const replaceDashCase = (input) => {
|
|
71
|
+
return input.replace(REPLACE_DASH_CASE_REGEXP, to);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/* CAMEL CASE */
|
|
75
|
+
const fromAsCamelCase = dashCaseToCamelCase(from);
|
|
76
|
+
const toAsCamelCase = dashCaseToCamelCase(to);
|
|
77
|
+
const REPLACE_CAMEL_CASE_REGEXP = new RegExp(fromAsCamelCase, 'g');
|
|
78
|
+
|
|
79
|
+
const replaceCamelCase = (input) => {
|
|
80
|
+
return input.replace(REPLACE_CAMEL_CASE_REGEXP, toAsCamelCase);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const replaceDashAndCamelCase = from.includes('-')
|
|
84
|
+
? (input) => {
|
|
85
|
+
return replaceCamelCase(replaceDashCase(input));
|
|
86
|
+
}
|
|
87
|
+
: replaceDashCase;
|
|
88
|
+
|
|
89
|
+
/* PASCAL CASE */
|
|
90
|
+
const fromAsPascalCase = dashCaseToPascalCase(from);
|
|
91
|
+
const toAsPascalCase = dashCaseToPascalCase(to);
|
|
92
|
+
const REPLACE_PASCAL_CASE_REGEXP = new RegExp(fromAsPascalCase, 'g');
|
|
93
|
+
|
|
94
|
+
const replacePascalCase = (input) => {
|
|
95
|
+
return input.replace(REPLACE_PASCAL_CASE_REGEXP, toAsPascalCase);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/* UPPER CASE */
|
|
99
|
+
|
|
100
|
+
const fromAsUpperCase = dashCaseToUpperCase(from);
|
|
101
|
+
const toAsUpperCase = dashCaseToUpperCase(to);
|
|
102
|
+
const REPLACE_UPPER_CASE_REGEXP = new RegExp(fromAsUpperCase, 'g');
|
|
103
|
+
|
|
104
|
+
const replaceUpperCase = (input) => {
|
|
105
|
+
return input.replace(REPLACE_UPPER_CASE_REGEXP, toAsUpperCase);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/* JOIN */
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @param {string} input
|
|
112
|
+
* @return {string}
|
|
113
|
+
*/
|
|
114
|
+
return (input) => {
|
|
115
|
+
return replaceUpperCase(replacePascalCase(replaceDashAndCamelCase(input)));
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/*---*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} entryPath
|
|
123
|
+
* @param {(input: string) => string} refactor
|
|
124
|
+
* @param {boolean} dry
|
|
125
|
+
* @return {Promise<string>}
|
|
126
|
+
*/
|
|
127
|
+
async function refactorFileName(entryPath, refactor, dry = false) {
|
|
128
|
+
const newPath = join(dirname(entryPath), refactor(basename(entryPath)));
|
|
129
|
+
if (entryPath !== newPath) {
|
|
130
|
+
if (dry) {
|
|
131
|
+
console.log(
|
|
132
|
+
chalk.blueBright('rename:'),
|
|
133
|
+
chalk.greenBright(basename(entryPath)),
|
|
134
|
+
chalk.blueBright('=>'),
|
|
135
|
+
chalk.yellowBright(basename(newPath)),
|
|
136
|
+
);
|
|
137
|
+
return entryPath;
|
|
138
|
+
} else {
|
|
139
|
+
await rename(entryPath, newPath);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return newPath;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param {string} entryPath
|
|
147
|
+
* @param {(input: string) => string} refactor
|
|
148
|
+
* @param {boolean} dry
|
|
149
|
+
* @return {Promise<string>}
|
|
150
|
+
*/
|
|
151
|
+
async function refactorFileContent(entryPath, refactor, dry = false) {
|
|
152
|
+
const content = await readFile(entryPath, { encoding: 'utf8' });
|
|
153
|
+
const newContent = refactor(content);
|
|
154
|
+
|
|
155
|
+
if (newContent !== content) {
|
|
156
|
+
if (dry) {
|
|
157
|
+
console.log(
|
|
158
|
+
chalk.blueBright('refactoring content of:'),
|
|
159
|
+
chalk.greenBright(basename(entryPath)),
|
|
160
|
+
);
|
|
161
|
+
// console.log(colorize('yellow', newContent));
|
|
162
|
+
} else {
|
|
163
|
+
await writeFile(entryPath, newContent, { encoding: 'utf8' });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { confirm, select } from '@inquirer/prompts';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { mkdir, readFile } from 'node:fs/promises';
|
|
4
|
+
import { join, relative } from 'node:path';
|
|
5
|
+
import { cmd } from '../helpers/cmd.js';
|
|
6
|
+
import { explore } from '../helpers/fs/explore.js';
|
|
7
|
+
import { readJsonFile } from '../helpers/fs/read-json-file.js';
|
|
8
|
+
import { writeFileSafe } from '../helpers/fs/write-file-safe.js';
|
|
9
|
+
import { writeJsonFile } from '../helpers/fs/write-json-file.js';
|
|
10
|
+
import { listAvailableTemplates } from '../helpers/list-available-templates.js';
|
|
11
|
+
import { log } from '../helpers/log/log.js';
|
|
12
|
+
import { ROOT_PATH, getTemplatePath } from '../helpers/paths.constant.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Upgrades an existing library.
|
|
16
|
+
*
|
|
17
|
+
* @return {Promise<void>}
|
|
18
|
+
*/
|
|
19
|
+
export async function upgrade({ force = false } = {}) {
|
|
20
|
+
const destinationPath = '.';
|
|
21
|
+
|
|
22
|
+
const version = (await readJsonFile(join(ROOT_PATH, 'package.json'))).version;
|
|
23
|
+
const destinationPackage = await readJsonFile(join(destinationPath, 'package.json'));
|
|
24
|
+
|
|
25
|
+
const templates = await listAvailableTemplates();
|
|
26
|
+
let type;
|
|
27
|
+
|
|
28
|
+
if (Object.hasOwn(destinationPackage, 'fabrique')) {
|
|
29
|
+
type = destinationPackage.fabrique.type;
|
|
30
|
+
|
|
31
|
+
if (!templates.includes(type)) {
|
|
32
|
+
throw new Error(`type must be one of the following: ${templates.join(', ')}.`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!force && destinationPackage.fabrique.version === version) {
|
|
36
|
+
log('warn', 'Library already up-to-date.');
|
|
37
|
+
if (
|
|
38
|
+
!(await confirm({
|
|
39
|
+
message: 'Force the update ?',
|
|
40
|
+
default: false,
|
|
41
|
+
}))
|
|
42
|
+
) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
if (!force) {
|
|
48
|
+
log('warn', 'Not a "fabrique" lib.');
|
|
49
|
+
if (
|
|
50
|
+
!(await confirm({
|
|
51
|
+
message: 'Upgrade anyway ?',
|
|
52
|
+
default: true,
|
|
53
|
+
}))
|
|
54
|
+
) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
type = await select({
|
|
59
|
+
message: 'Select a type',
|
|
60
|
+
choices: templates.map((template) => {
|
|
61
|
+
return {
|
|
62
|
+
value: template,
|
|
63
|
+
};
|
|
64
|
+
}),
|
|
65
|
+
default: templates[0],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
console.log(chalk.magentaBright('Upgrading the library...'));
|
|
70
|
+
|
|
71
|
+
const templatePath = getTemplatePath(type);
|
|
72
|
+
|
|
73
|
+
await Promise.all([
|
|
74
|
+
upgradeFiles(templatePath, destinationPath),
|
|
75
|
+
upgradePackage({
|
|
76
|
+
templatePath,
|
|
77
|
+
destinationPath,
|
|
78
|
+
destinationPackage,
|
|
79
|
+
fabriqueConfig: {
|
|
80
|
+
version,
|
|
81
|
+
type,
|
|
82
|
+
},
|
|
83
|
+
}),
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
// update dependencies
|
|
87
|
+
await cmd('yarn', [], {
|
|
88
|
+
cwd: destinationPath,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
log('success', `Library ${JSON.stringify(destinationPackage.name)} updated.`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function upgradeFiles(templatePath, destinationPath) {
|
|
95
|
+
// files to exclude
|
|
96
|
+
const exclude = [
|
|
97
|
+
/^src(?:\/.*)?$/,
|
|
98
|
+
/^README\.md$/,
|
|
99
|
+
/^LICENSE$/,
|
|
100
|
+
/^package\.json$/,
|
|
101
|
+
/^yarn\.lock$/,
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
for await (const [path, stats] of explore(templatePath)) {
|
|
105
|
+
const entryRelativePath = relative(templatePath, path);
|
|
106
|
+
const entryDestinationPath = join(destinationPath, entryRelativePath);
|
|
107
|
+
|
|
108
|
+
if (exclude.every((exclude) => !exclude.test(entryRelativePath))) {
|
|
109
|
+
if (stats.isFile()) {
|
|
110
|
+
console.log(chalk.yellowBright('override:'), entryRelativePath);
|
|
111
|
+
await writeFileSafe(entryDestinationPath, await readFile(path, { encoding: 'utf8' }));
|
|
112
|
+
} else if (stats.isDirectory()) {
|
|
113
|
+
await mkdir(entryDestinationPath, { recursive: true });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Upgrades the destination `package.json`.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} templatePath
|
|
123
|
+
* @param {string} destinationPath
|
|
124
|
+
* @param {any} destinationPackage
|
|
125
|
+
* @param {{version: string; type: string;}} fabriqueConfig
|
|
126
|
+
* @return {Promise<void>}
|
|
127
|
+
*/
|
|
128
|
+
async function upgradePackage({
|
|
129
|
+
templatePath,
|
|
130
|
+
destinationPath,
|
|
131
|
+
destinationPackage,
|
|
132
|
+
fabriqueConfig,
|
|
133
|
+
}) {
|
|
134
|
+
const templatePackage = await readJsonFile(join(templatePath, 'package.json'));
|
|
135
|
+
|
|
136
|
+
const newPackageJson = {
|
|
137
|
+
...templatePackage,
|
|
138
|
+
...destinationPackage,
|
|
139
|
+
scripts: {
|
|
140
|
+
...Object.fromEntries(
|
|
141
|
+
Object.entries(destinationPackage.scripts ?? {}).filter(([key, value]) => {
|
|
142
|
+
return !key.startsWith('fb:');
|
|
143
|
+
}),
|
|
144
|
+
),
|
|
145
|
+
...templatePackage.scripts,
|
|
146
|
+
},
|
|
147
|
+
devDependencies: {
|
|
148
|
+
...destinationPackage.devDependencies,
|
|
149
|
+
...templatePackage.devDependencies,
|
|
150
|
+
},
|
|
151
|
+
packageManager: templatePackage.packageManager,
|
|
152
|
+
fabrique: fabriqueConfig,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
console.log(chalk.yellowBright('merge:'), 'package.json');
|
|
156
|
+
await writeJsonFile(join(destinationPath, 'package.json'), newPackageJson);
|
|
157
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { cmd } from '../helpers/cmd.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lanches verdaccio
|
|
5
|
+
*/
|
|
6
|
+
export async function verdaccio() {
|
|
7
|
+
/*
|
|
8
|
+
import { dirname } from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
const cwd = dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
*/
|
|
12
|
+
// lsof -i tcp:4873 | awk '/4873/{print $2}' | xargs kill
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await cmd('verdaccio', ['--listen', '0.0.0.0:4873']);
|
|
16
|
+
} catch {
|
|
17
|
+
await cmd('npm', ['install', '--global', 'verdaccio']);
|
|
18
|
+
await cmd('verdaccio', ['--listen', '0.0.0.0:4873']);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
shell: process.platform === 'win32',
|
|
16
|
+
detached: false,
|
|
17
|
+
stdio: 'inherit',
|
|
18
|
+
...options,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
childProcess.on('exit', (code) => {
|
|
22
|
+
if (code === 0) {
|
|
23
|
+
resolve();
|
|
24
|
+
} else {
|
|
25
|
+
reject(new Error(`Exit with code ${code}.`));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
childProcess.on('error', (err) => {
|
|
30
|
+
reject(err);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// process.on('SIGINT', () => {
|
|
34
|
+
// childProcess.kill('SIGINT');
|
|
35
|
+
// });
|
|
36
|
+
//
|
|
37
|
+
// process.on('SIGTERM', () => {
|
|
38
|
+
// childProcess.kill('SIGTERM');
|
|
39
|
+
// });
|
|
40
|
+
//
|
|
41
|
+
// process.on('SIGBREAK', () => {
|
|
42
|
+
// childProcess.kill('SIGBREAK');
|
|
43
|
+
// });
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { lstat, readdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} path
|
|
6
|
+
* @return {AsyncGenerator<readonly [path: string, stats: Stats]>}
|
|
7
|
+
*/
|
|
8
|
+
export async function* explore(path) {
|
|
9
|
+
const stats = await lstat(path);
|
|
10
|
+
yield [path, stats];
|
|
11
|
+
|
|
12
|
+
if (stats.isDirectory()) {
|
|
13
|
+
const files = await readdir(path);
|
|
14
|
+
for (const file of files) {
|
|
15
|
+
yield* explore(join(path, file));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|