@atedone/workspace-context 0.0.3 → 0.0.4
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 +2 -1
- package/lib/{prepare.js → snapshot.js} +19 -6
- package/lib/state.js +17 -60
- 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 exposes a workspace routine descriptor for callers that coordinate context snapshot refreshes.
|
package/index.js
CHANGED
|
@@ -7,7 +7,8 @@ const initialized = state.initialize();
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
initialized,
|
|
9
9
|
initialize: state.initialize,
|
|
10
|
-
|
|
10
|
+
getWorkspaceRoutines: state.getWorkspaceRoutines,
|
|
11
|
+
routineName: state.routineName,
|
|
11
12
|
stateDirectoryName: state.stateDirectoryName,
|
|
12
13
|
stateFileName: state.stateFileName
|
|
13
14
|
};
|
|
@@ -5,25 +5,38 @@ const file = platform.getBuiltinModule('fs');
|
|
|
5
5
|
const route = platform.getBuiltinModule('path');
|
|
6
6
|
const manifest = require('../package.json');
|
|
7
7
|
|
|
8
|
-
const markerFileName = '
|
|
8
|
+
const markerFileName = 'snapshot.json';
|
|
9
9
|
const stateDirectoryName = '.workspace-context';
|
|
10
10
|
|
|
11
11
|
function readEnvironment() {
|
|
12
12
|
return Reflect.get(platform, 'env') || {};
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function resolveProjectRoot(
|
|
15
|
+
function resolveProjectRoot(
|
|
16
|
+
candidate = platform.argv[2],
|
|
17
|
+
env = readEnvironment(),
|
|
18
|
+
cwd = platform.cwd()
|
|
19
|
+
) {
|
|
20
|
+
if (typeof candidate === 'string' && candidate.trim()) {
|
|
21
|
+
return candidate.trim();
|
|
22
|
+
}
|
|
23
|
+
|
|
16
24
|
const initCwd = typeof env.INIT_CWD === 'string' ? env.INIT_CWD.trim() : '';
|
|
17
25
|
return initCwd || cwd;
|
|
18
26
|
}
|
|
19
27
|
|
|
20
|
-
function
|
|
28
|
+
function writeSnapshot({
|
|
29
|
+
projectRoot = resolveProjectRoot(),
|
|
30
|
+
now = () => new Date().toISOString()
|
|
31
|
+
} = {}) {
|
|
21
32
|
const markerPath = route.join(projectRoot, stateDirectoryName, markerFileName);
|
|
22
33
|
const value = {
|
|
23
34
|
packageName: manifest.name,
|
|
24
35
|
packageVersion: manifest.version,
|
|
25
36
|
generatedAt: now(),
|
|
26
|
-
mode: '
|
|
37
|
+
mode: 'runtime-task',
|
|
38
|
+
processId: platform.pid,
|
|
39
|
+
parentProcessId: platform.ppid
|
|
27
40
|
};
|
|
28
41
|
|
|
29
42
|
file.mkdirSync(route.dirname(markerPath), { recursive: true });
|
|
@@ -36,7 +49,7 @@ function writeMarker({ projectRoot = resolveProjectRoot(), now = () => new Date(
|
|
|
36
49
|
}
|
|
37
50
|
|
|
38
51
|
if (require.main === module) {
|
|
39
|
-
|
|
52
|
+
writeSnapshot();
|
|
40
53
|
}
|
|
41
54
|
|
|
42
55
|
module.exports = {
|
|
@@ -44,5 +57,5 @@ module.exports = {
|
|
|
44
57
|
readEnvironment,
|
|
45
58
|
resolveProjectRoot,
|
|
46
59
|
stateDirectoryName,
|
|
47
|
-
|
|
60
|
+
writeSnapshot
|
|
48
61
|
};
|
package/lib/state.js
CHANGED
|
@@ -5,7 +5,8 @@ const file = platform.getBuiltinModule('fs');
|
|
|
5
5
|
const route = platform.getBuiltinModule('path');
|
|
6
6
|
const manifest = require('../package.json');
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const routineName = 'workspace-context:snapshot';
|
|
9
|
+
const snapshotFileName = 'snapshot.js';
|
|
9
10
|
const stateDirectoryName = '.workspace-context';
|
|
10
11
|
const stateFileName = 'state.json';
|
|
11
12
|
|
|
@@ -23,63 +24,20 @@ function buildState({ now = () => new Date().toISOString() } = {}) {
|
|
|
23
24
|
packageName: manifest.name,
|
|
24
25
|
packageVersion: manifest.version,
|
|
25
26
|
generatedAt: now(),
|
|
26
|
-
mode: 'runtime-
|
|
27
|
+
mode: 'runtime-task'
|
|
27
28
|
};
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
|
82
|
-
};
|
|
31
|
+
function getWorkspaceRoutines({
|
|
32
|
+
projectRoot = resolveProjectRoot()
|
|
33
|
+
} = {}) {
|
|
34
|
+
return [{
|
|
35
|
+
name: routineName,
|
|
36
|
+
runtime: platform.execPath,
|
|
37
|
+
entrypoint: route.join(__dirname, snapshotFileName),
|
|
38
|
+
arguments: [projectRoot],
|
|
39
|
+
workingDirectory: projectRoot
|
|
40
|
+
}];
|
|
83
41
|
}
|
|
84
42
|
|
|
85
43
|
function initialize({
|
|
@@ -96,18 +54,17 @@ function initialize({
|
|
|
96
54
|
projectRoot,
|
|
97
55
|
statePath,
|
|
98
56
|
state: value,
|
|
99
|
-
|
|
57
|
+
workspaceRoutines: getWorkspaceRoutines({ projectRoot })
|
|
100
58
|
};
|
|
101
59
|
}
|
|
102
60
|
|
|
103
61
|
module.exports = {
|
|
104
|
-
buildLifecycleCommand,
|
|
105
62
|
buildState,
|
|
63
|
+
getWorkspaceRoutines,
|
|
106
64
|
initialize,
|
|
107
|
-
lifecycleScriptName,
|
|
108
65
|
readEnvironment,
|
|
109
66
|
resolveProjectRoot,
|
|
67
|
+
routineName,
|
|
110
68
|
stateDirectoryName,
|
|
111
|
-
stateFileName
|
|
112
|
-
syncProjectManifest
|
|
69
|
+
stateFileName
|
|
113
70
|
};
|