eject 1.0.1 → 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 +4 -0
- package/README.md +3 -0
- package/package.json +3 -2
- package/src/command.ts +17 -23
- package/src/index.ts +1 -1
- package/src/system.ts +66 -6
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
> [!WARNING]
|
|
2
|
+
> This package is work-in-progress and doesn't work yet.
|
|
3
|
+
|
|
1
4
|
# Eject
|
|
2
5
|
|
|
3
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.
|
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
|
@@ -9,13 +9,14 @@ import {
|
|
|
9
9
|
import { errorLog, infoLog, warnLog } from './log.js';
|
|
10
10
|
import {
|
|
11
11
|
copyDependency,
|
|
12
|
+
dependencyManagerAction,
|
|
12
13
|
detectPackageManager,
|
|
13
14
|
updatePackageJson,
|
|
14
15
|
} from './system.js';
|
|
15
16
|
import { execSync } from 'child_process';
|
|
16
17
|
|
|
17
18
|
export function eject(
|
|
18
|
-
|
|
19
|
+
dependency: string,
|
|
19
20
|
options: { force: boolean; verbose: boolean },
|
|
20
21
|
) {
|
|
21
22
|
if (!options.force && !isGitInstalled()) {
|
|
@@ -30,34 +31,27 @@ export function eject(
|
|
|
30
31
|
);
|
|
31
32
|
return;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (options.verbose) {
|
|
41
|
-
errorLog('Error ejecting dependency:', dependency);
|
|
42
|
-
}
|
|
43
|
-
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);
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
chalk.bold(successFullEjections),
|
|
50
|
-
);
|
|
43
|
+
|
|
44
|
+
if (successfulEjected) {
|
|
45
|
+
console.log('✅ Ejected dependencies:', chalk.bold(dependency));
|
|
51
46
|
} else {
|
|
52
|
-
|
|
47
|
+
console.log('❌ Ejected dependencies: ', chalk.bold(dependency));
|
|
53
48
|
return;
|
|
54
49
|
}
|
|
55
|
-
if (failedEjections.length > 0) {
|
|
56
|
-
console.log('❌ Ejected dependencies: ', chalk.bold(failedEjections));
|
|
57
|
-
}
|
|
58
|
-
updatePackageJson(successFullEjections);
|
|
59
|
-
commitEjection(config.COMMIT_MESSAGE);
|
|
60
50
|
const { packageManager, lockFile } = detectPackageManager();
|
|
51
|
+
updatePackageJson(dependency, packageManager === 'pnpm' ? 'link' : 'file');
|
|
52
|
+
commitEjection(config.COMMIT_MESSAGE);
|
|
53
|
+
const needsToCommit = dependencyManagerAction(packageManager);
|
|
54
|
+
if (needsToCommit) amendCommit();
|
|
61
55
|
const installCmd = `${packageManager} install`;
|
|
62
56
|
const installLog = chalk.bold(installCmd);
|
|
63
57
|
console.log(`📦 Running ${installLog} to update ${chalk.bold(lockFile)}`);
|
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
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;
|
|
@@ -98,3 +105,56 @@ export function detectPackageManager(
|
|
|
98
105
|
assert(packageManager);
|
|
99
106
|
return { packageManager, lockFile };
|
|
100
107
|
}
|
|
108
|
+
|
|
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,
|
|
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
|
+
}
|