fabrique 0.0.1 → 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/.editorconfig +7 -0
- package/.nvmrc +1 -0
- package/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/README.md +218 -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,218 @@
|
|
|
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
|
+
> `fabrique` is the French word for "made/factory"
|
|
25
|
+
|
|
26
|
+
## Motivation
|
|
27
|
+
|
|
28
|
+
When we develop javascript/typescript libraries, we frequently create new repositories,
|
|
29
|
+
and maintains the scripts to build, test, and debug them.
|
|
30
|
+
This tends to become rapidly cumbersome, especially when their number grows.
|
|
31
|
+
We may choose to opt in for a _monorepo_, and I'll tell you: yes, in many cases, this is the optimal solution.
|
|
32
|
+
However, in many other cases, one library per-repo makes sense, and this is where `fabrique` comes in.
|
|
33
|
+
|
|
34
|
+
Another goal is to focus on simplicity: for new incomers in javascript,
|
|
35
|
+
learning all the tools and build processes is complicated.
|
|
36
|
+
You want to write your library, but you have to learn `vite`, `jest`, how to build and publish a lib,
|
|
37
|
+
install `prettier`, etc. With `fabrique` all comes in pre-configured.
|
|
38
|
+
|
|
39
|
+
## 📝 Documentation
|
|
40
|
+
|
|
41
|
+
### Requirements
|
|
42
|
+
|
|
43
|
+
The library requires [Node.js](https://nodejs.org/en) `22+` and [yarn](https://yarnpkg.com/) `4+`.
|
|
44
|
+
|
|
45
|
+
### Installation
|
|
46
|
+
|
|
47
|
+
We recommend to use [npx](https://docs.npmjs.com/cli/v8/commands/npx):
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
npx fabrique CMD
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
But, you may prefer to install globally `fabrique`:
|
|
54
|
+
|
|
55
|
+
```shell
|
|
56
|
+
npm install -g fabrique
|
|
57
|
+
```
|
|
58
|
+
And run a command with:
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
fabrique CMD
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
### List of commands
|
|
66
|
+
|
|
67
|
+
#### create
|
|
68
|
+
|
|
69
|
+
```shell
|
|
70
|
+
npx fabrique upgrade lib [name]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
##### example
|
|
74
|
+
|
|
75
|
+
```shell
|
|
76
|
+
npx fabrique create @my-company/my-library
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
##### action
|
|
80
|
+
|
|
81
|
+
This command creates a new library with the name `[name]` inside the folder `./[name]` (in our example: `./@my-company/my-library`).
|
|
82
|
+
You'll be prompted for a description, author and git url.
|
|
83
|
+
|
|
84
|
+
> INFO: run the command where you want to create your project.
|
|
85
|
+
|
|
86
|
+
##### options
|
|
87
|
+
|
|
88
|
+
- `[name]`: the library name
|
|
89
|
+
|
|
90
|
+
#### upgrade
|
|
91
|
+
|
|
92
|
+
```shell
|
|
93
|
+
npx fabrique upgrade
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
##### action
|
|
97
|
+
|
|
98
|
+
This command updates an existing `fabrique` project: it updates the build files, the scripts, etc.
|
|
99
|
+
It tries to be non-destructive.
|
|
100
|
+
|
|
101
|
+
> INFO: run the command **inside** the `fabrique` project.
|
|
102
|
+
|
|
103
|
+
##### options
|
|
104
|
+
|
|
105
|
+
- `--force`: forces an upgrade even if the lib is not a `fabrique` project or if the version is identical. _Use with caution._
|
|
106
|
+
|
|
107
|
+
#### refactor
|
|
108
|
+
|
|
109
|
+
```shell
|
|
110
|
+
npx fabrique refactor [from] [to]
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
##### example
|
|
114
|
+
|
|
115
|
+
```shell
|
|
116
|
+
npx fabrique refactor my-component my-new-component
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> Refactors all files and directories containing `my-component` as name into `my-new-component` (recursively).
|
|
120
|
+
> And all text content `my-component` (or derived cases) into `my-new-component` (keeping the same case).
|
|
121
|
+
|
|
122
|
+
If we run it with the following files:
|
|
123
|
+
|
|
124
|
+
- some-component
|
|
125
|
+
- my-component
|
|
126
|
+
- my-component.ts
|
|
127
|
+
- my-component-abc
|
|
128
|
+
- my-component-abc.ts
|
|
129
|
+
|
|
130
|
+
We get:
|
|
131
|
+
|
|
132
|
+
- some-component
|
|
133
|
+
- my-new-component
|
|
134
|
+
- my-new-component.ts
|
|
135
|
+
- my-new-component-abc
|
|
136
|
+
- my-new-component-abc.ts
|
|
137
|
+
|
|
138
|
+
And with this file content:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
class MyComponent {}
|
|
142
|
+
|
|
143
|
+
// my-component
|
|
144
|
+
const MY_COMPONENT = null;
|
|
145
|
+
function my_component(myComponent: MyComponent) {}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
We get:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
class MyNewComponent {}
|
|
152
|
+
|
|
153
|
+
// my-new-component
|
|
154
|
+
const MY_NEW_COMPONENT = null;
|
|
155
|
+
function my_new_component(myNewComponent: MyNewComponent) {}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
##### action
|
|
159
|
+
|
|
160
|
+
This command refactors files and directories recursively from the `cwd` (by default, the current directory in which the script is executed).
|
|
161
|
+
It preserves the case of the names (ex: `dash-case`, or `cameCase`).
|
|
162
|
+
|
|
163
|
+
> INFO: run the command inside the folder that you want to refactor.
|
|
164
|
+
|
|
165
|
+
##### options
|
|
166
|
+
|
|
167
|
+
- `[from]`: the "source" text to refactor. Must be `dash-case`.
|
|
168
|
+
- `[to]`: the "destination" text to refactor. Must be `dash-case`.
|
|
169
|
+
- `--dry` (default: false): runs without modifying the files. This is useful to check if your refactoring is safe or not.
|
|
170
|
+
- `--cwd` (default: current folder): specifies the directory to start from.
|
|
171
|
+
|
|
172
|
+
#### verdaccio
|
|
173
|
+
|
|
174
|
+
```shell
|
|
175
|
+
npx fabrique verdaccio
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Installs and lauches [verdaccio](https://verdaccio.org/). This is useful to debug interdependent libraries.
|
|
179
|
+
|
|
180
|
+
Let's say we have:
|
|
181
|
+
|
|
182
|
+
- a library `my-lib-a`
|
|
183
|
+
- a library `my-lib-b` with `my-lib-a` as `dependency`
|
|
184
|
+
|
|
185
|
+
If we're working on `my-lib-a`, and want to test if it works on `my-lib-b`, we'll publish a `dev` version of `my-lib-a` on a local `verdaccio`.
|
|
186
|
+
And consume this dev version on `my-lib-b`.
|
|
187
|
+
|
|
188
|
+
> We choose verdaccio instead of `npm link` because versions and dev packages are unique with a package registry.
|
|
189
|
+
>
|
|
190
|
+
> Thus, we may have `my-lib-b` consuming `my-lib-a-dev.0` and `my-lib-c` consuming `my-lib-a-dev.1`, which is not possible with `npm link`.
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
#### version
|
|
194
|
+
|
|
195
|
+
```shell
|
|
196
|
+
npx fabrique --version
|
|
197
|
+
# or
|
|
198
|
+
npx fabrique -v
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Returns the current `fabrique` version.
|
|
202
|
+
|
|
203
|
+
#### help
|
|
204
|
+
|
|
205
|
+
```shell
|
|
206
|
+
npx fabrique --help
|
|
207
|
+
# or
|
|
208
|
+
npx fabrique -h
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
You may get help on individual commands:
|
|
212
|
+
|
|
213
|
+
```shell
|
|
214
|
+
npx fabrique create -h
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
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",
|
|
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
|
+
}
|