eject 1.0.0 → 1.0.2
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/CHANGELOG.md +13 -0
- package/README.md +4 -1
- package/package.json +3 -2
- package/src/command.ts +28 -28
- package/src/index.ts +1 -1
- package/src/system.ts +83 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
## [1.0.2](https://github.com/snake-py/eject/compare/v1.0.1...v1.0.2) (2024-11-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## [1.0.1](https://github.com/snake-py/eject/compare/v1.0.0...v1.0.1) (2024-11-15)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* use `link:` protocol instead of `file:` ([40cbab3](https://github.com/snake-py/eject/commit/40cbab318c94ebf9953e90ffa4f9b739e0282f80))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
1
14
|
# 1.0.0 (2024-11-15)
|
|
2
15
|
|
|
3
16
|
|
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
> [!WARNING]
|
|
2
|
+
> This package is work-in-progress and doesn't work yet.
|
|
3
|
+
|
|
1
4
|
# Eject
|
|
2
5
|
|
|
3
|
-
This package is written to
|
|
6
|
+
This package is written to eject dependencies from your project. It will copy all the files and folders to an `ejected` folder and remove it from node_modules. It will automatically update the package.json file an link the dependencies to the ejected folder.
|
|
4
7
|
|
|
5
8
|
|
|
6
9
|
## Usage
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eject",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "This package helps you to eject dependencies from node_modules easily",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"homepage": "https://github.com/snake-py/eject#readme",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"cac": "^6.7.14",
|
|
22
|
-
"chalk": "^5.3.0"
|
|
22
|
+
"chalk": "^5.3.0",
|
|
23
|
+
"yaml": "^2.6.1"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@brillout/release-me": "^0.4.2",
|
package/src/command.ts
CHANGED
|
@@ -7,10 +7,16 @@ import {
|
|
|
7
7
|
isGitInstalled,
|
|
8
8
|
} from './git.js';
|
|
9
9
|
import { errorLog, infoLog, warnLog } from './log.js';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
copyDependency,
|
|
12
|
+
dependencyManagerAction,
|
|
13
|
+
detectPackageManager,
|
|
14
|
+
updatePackageJson,
|
|
15
|
+
} from './system.js';
|
|
16
|
+
import { execSync } from 'child_process';
|
|
11
17
|
|
|
12
18
|
export function eject(
|
|
13
|
-
|
|
19
|
+
dependency: string,
|
|
14
20
|
options: { force: boolean; verbose: boolean },
|
|
15
21
|
) {
|
|
16
22
|
if (!options.force && !isGitInstalled()) {
|
|
@@ -25,38 +31,32 @@ export function eject(
|
|
|
25
31
|
);
|
|
26
32
|
return;
|
|
27
33
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (options.verbose) {
|
|
36
|
-
errorLog('Error ejecting dependency:', dependency);
|
|
37
|
-
}
|
|
38
|
-
failedEjections.push(dependency);
|
|
34
|
+
let successfulEjected = false;
|
|
35
|
+
try {
|
|
36
|
+
copyDependency(dependency);
|
|
37
|
+
successfulEjected = true;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (options.verbose) {
|
|
40
|
+
errorLog('Error ejecting dependency:', dependency);
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
chalk.bold(successFullEjections),
|
|
45
|
-
);
|
|
43
|
+
|
|
44
|
+
if (successfulEjected) {
|
|
45
|
+
console.log('✅ Ejected dependencies:', chalk.bold(dependency));
|
|
46
46
|
} else {
|
|
47
|
-
|
|
47
|
+
console.log('❌ Ejected dependencies: ', chalk.bold(dependency));
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
updatePackageJson(successFullEjections);
|
|
50
|
+
const { packageManager, lockFile } = detectPackageManager();
|
|
51
|
+
updatePackageJson(dependency, packageManager === 'pnpm' ? 'link' : 'file');
|
|
54
52
|
commitEjection(config.COMMIT_MESSAGE);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
console.log(
|
|
53
|
+
const needsToCommit = dependencyManagerAction(packageManager);
|
|
54
|
+
if (needsToCommit) amendCommit();
|
|
55
|
+
const installCmd = `${packageManager} install`;
|
|
56
|
+
const installLog = chalk.bold(installCmd);
|
|
57
|
+
console.log(`📦 Running ${installLog} to update ${chalk.bold(lockFile)}`);
|
|
58
|
+
execSync(installCmd, { stdio: 'inherit' });
|
|
59
|
+
console.log(`✅ ${installLog} done`);
|
|
60
60
|
amendCommit();
|
|
61
61
|
console.log(
|
|
62
62
|
'➡️ Run',
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { config } from './config.js';
|
|
|
5
5
|
|
|
6
6
|
const cli = cac('eject');
|
|
7
7
|
|
|
8
|
-
cli.command('
|
|
8
|
+
cli.command('<dependencies>', 'Ejects a dependency from node_modules', {
|
|
9
9
|
allowUnknownOptions: true,
|
|
10
10
|
})
|
|
11
11
|
.option('-f, --force', 'Bypass git history check')
|
package/src/system.ts
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { resolve } from 'path';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import
|
|
3
|
+
import assert from 'assert';
|
|
4
|
+
import { parse, stringify } from 'yaml';
|
|
4
5
|
|
|
5
|
-
export function updatePackageJson(
|
|
6
|
+
export function updatePackageJson(
|
|
7
|
+
ejectedDependency: string,
|
|
8
|
+
protocol: string = 'file',
|
|
9
|
+
) {
|
|
6
10
|
const packageJson = JSON.parse(
|
|
7
11
|
fs.readFileSync(resolve('./package.json'), 'utf-8'),
|
|
8
12
|
);
|
|
9
13
|
packageJson['devDependencies'] = setPathAsVersion(
|
|
10
14
|
packageJson['devDependencies'],
|
|
11
|
-
|
|
15
|
+
ejectedDependency,
|
|
16
|
+
protocol,
|
|
12
17
|
);
|
|
13
18
|
packageJson['dependencies'] = setPathAsVersion(
|
|
14
19
|
packageJson['dependencies'],
|
|
15
|
-
|
|
20
|
+
ejectedDependency,
|
|
21
|
+
protocol,
|
|
16
22
|
);
|
|
17
23
|
fs.writeFileSync(
|
|
18
24
|
resolve('./package.json'),
|
|
@@ -23,14 +29,15 @@ export function updatePackageJson(successFullEjections: string[]) {
|
|
|
23
29
|
|
|
24
30
|
function setPathAsVersion(
|
|
25
31
|
dependencies: Record<string, string>,
|
|
26
|
-
|
|
32
|
+
ejectedDependency: string,
|
|
33
|
+
protocol: string = 'file',
|
|
27
34
|
) {
|
|
28
35
|
const updatedDependencies: Record<string, string> = {};
|
|
29
36
|
for (const key in dependencies) {
|
|
30
|
-
if (
|
|
37
|
+
if (ejectedDependency !== key) {
|
|
31
38
|
updatedDependencies[key] = dependencies[key] as string;
|
|
32
39
|
} else {
|
|
33
|
-
updatedDependencies[key] =
|
|
40
|
+
updatedDependencies[key] = `${protocol}:./ejected/${key}`;
|
|
34
41
|
}
|
|
35
42
|
}
|
|
36
43
|
return updatedDependencies;
|
|
@@ -56,12 +63,10 @@ export function copyDependency(dependency: string) {
|
|
|
56
63
|
});
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
export function
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const detectPackageManager = (currentDir: string = './', depth: number = 0) => {
|
|
66
|
+
export function detectPackageManager(
|
|
67
|
+
currentDir: string = './',
|
|
68
|
+
depth: number = 0,
|
|
69
|
+
) {
|
|
65
70
|
if (depth > 20) {
|
|
66
71
|
throw new Error('No lock file found');
|
|
67
72
|
}
|
|
@@ -85,17 +90,71 @@ const detectPackageManager = (currentDir: string = './', depth: number = 0) => {
|
|
|
85
90
|
if (foundLockFiles.length > 1) {
|
|
86
91
|
throw new Error('Multiple lock files found');
|
|
87
92
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
|
|
94
|
+
const lockFile = foundLockFiles[0] as string;
|
|
95
|
+
let packageManager: string | undefined;
|
|
96
|
+
if (lockFile === 'yarn.lock') {
|
|
97
|
+
packageManager = 'yarn';
|
|
98
|
+
} else if (lockFile === 'package-lock.json') {
|
|
99
|
+
packageManager = 'npm';
|
|
100
|
+
} else if (lockFile === 'pnpm-lock.yaml') {
|
|
101
|
+
packageManager = 'pnpm';
|
|
102
|
+
} else if (lockFile === 'bun.lock') {
|
|
103
|
+
packageManager = 'bun';
|
|
98
104
|
}
|
|
105
|
+
assert(packageManager);
|
|
106
|
+
return { packageManager, lockFile };
|
|
107
|
+
}
|
|
99
108
|
|
|
100
|
-
|
|
109
|
+
function detectWorkspaceFile(currentDir: string = './', depth: number = 0) {
|
|
110
|
+
if (depth > 20) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const workspaceFiles = ['pnpm-workspace.yaml', 'yarn-workspace.json'];
|
|
115
|
+
|
|
116
|
+
const foundWorkspaceFiles = workspaceFiles.filter((workspaceFile) =>
|
|
117
|
+
fs.existsSync(resolve(currentDir, workspaceFile)),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
if (foundWorkspaceFiles.length === 0) {
|
|
121
|
+
return detectWorkspaceFile(resolve(currentDir, '..'), depth + 1);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (foundWorkspaceFiles.length > 1) {
|
|
125
|
+
throw new Error('Multiple workspace files found');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return foundWorkspaceFiles[0];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function pnpm() {
|
|
132
|
+
const workspaceFile = detectWorkspaceFile();
|
|
133
|
+
if (!workspaceFile) {
|
|
134
|
+
fs.writeFileSync(
|
|
135
|
+
'./pnpm-workspace.yaml',
|
|
136
|
+
stringify({ packages: [`ejected/*`] }),
|
|
137
|
+
);
|
|
138
|
+
return true;
|
|
139
|
+
} else {
|
|
140
|
+
const workspace = parse(
|
|
141
|
+
fs.readFileSync(resolve('./', workspaceFile), 'utf-8'),
|
|
142
|
+
);
|
|
143
|
+
if (workspace.packages.includes('ejected/*')) return false;
|
|
144
|
+
workspace.packages.push('ejected/*');
|
|
145
|
+
fs.writeFileSync(resolve('./', workspaceFile), stringify(workspace));
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const action = {
|
|
151
|
+
pnpm,
|
|
101
152
|
};
|
|
153
|
+
|
|
154
|
+
export function dependencyManagerAction(packageManager: string) {
|
|
155
|
+
if (packageManager in action) {
|
|
156
|
+
return action[packageManager as keyof typeof action]();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return false;
|
|
160
|
+
}
|