@atedone/workspace-context 0.0.2 → 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 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 looks for an optional `workspace.context.js` file in the project root. When present, the file can export an `initialize` hook to extend workspace context setup.
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
- configFileName: state.configFileName,
10
+ getWorkspaceRoutines: state.getWorkspaceRoutines,
11
+ routineName: state.routineName,
11
12
  stateDirectoryName: state.stateDirectoryName,
12
13
  stateFileName: state.stateFileName
13
14
  };
@@ -0,0 +1,61 @@
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 = 'snapshot.json';
9
+ const stateDirectoryName = '.workspace-context';
10
+
11
+ function readEnvironment() {
12
+ return Reflect.get(platform, 'env') || {};
13
+ }
14
+
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
+
24
+ const initCwd = typeof env.INIT_CWD === 'string' ? env.INIT_CWD.trim() : '';
25
+ return initCwd || cwd;
26
+ }
27
+
28
+ function writeSnapshot({
29
+ projectRoot = resolveProjectRoot(),
30
+ now = () => new Date().toISOString()
31
+ } = {}) {
32
+ const markerPath = route.join(projectRoot, stateDirectoryName, markerFileName);
33
+ const value = {
34
+ packageName: manifest.name,
35
+ packageVersion: manifest.version,
36
+ generatedAt: now(),
37
+ mode: 'runtime-task',
38
+ processId: platform.pid,
39
+ parentProcessId: platform.ppid
40
+ };
41
+
42
+ file.mkdirSync(route.dirname(markerPath), { recursive: true });
43
+ file.writeFileSync(markerPath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
44
+
45
+ return {
46
+ markerPath,
47
+ marker: value
48
+ };
49
+ }
50
+
51
+ if (require.main === module) {
52
+ writeSnapshot();
53
+ }
54
+
55
+ module.exports = {
56
+ markerFileName,
57
+ readEnvironment,
58
+ resolveProjectRoot,
59
+ stateDirectoryName,
60
+ writeSnapshot
61
+ };
package/lib/state.js CHANGED
@@ -3,10 +3,10 @@
3
3
  const platform = process;
4
4
  const file = platform.getBuiltinModule('fs');
5
5
  const route = platform.getBuiltinModule('path');
6
- const runtime = platform.getBuiltinModule('module');
7
6
  const manifest = require('../package.json');
8
7
 
9
- const configFileName = 'workspace.context.js';
8
+ const routineName = 'workspace-context:snapshot';
9
+ const snapshotFileName = 'snapshot.js';
10
10
  const stateDirectoryName = '.workspace-context';
11
11
  const stateFileName = 'state.json';
12
12
 
@@ -24,48 +24,20 @@ function buildState({ now = () => new Date().toISOString() } = {}) {
24
24
  packageName: manifest.name,
25
25
  packageVersion: manifest.version,
26
26
  generatedAt: now(),
27
- mode: 'runtime-config'
27
+ mode: 'runtime-task'
28
28
  };
29
29
  }
30
30
 
31
- function loadProjectConfig({ projectRoot, statePath, state }) {
32
- const configPath = route.join(projectRoot, configFileName);
33
- if (!file.existsSync(configPath)) {
34
- return {
35
- configPath,
36
- loaded: false,
37
- hookExecuted: false,
38
- result: null
39
- };
40
- }
41
-
42
- const loader = runtime.createRequire(__filename);
43
- const config = Reflect.apply(loader, undefined, [configPath]);
44
- const hook = resolveHook(config);
45
-
46
- if (!hook) {
47
- return {
48
- configPath,
49
- loaded: true,
50
- hookExecuted: false,
51
- result: null
52
- };
53
- }
54
-
55
- const result = Reflect.apply(hook, config, [{
56
- packageName: manifest.name,
57
- packageVersion: manifest.version,
58
- projectRoot,
59
- statePath,
60
- state
61
- }]);
62
-
63
- return {
64
- configPath,
65
- loaded: true,
66
- hookExecuted: true,
67
- result: result === undefined ? null : result
68
- };
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
+ }];
69
41
  }
70
42
 
71
43
  function initialize({
@@ -82,33 +54,17 @@ function initialize({
82
54
  projectRoot,
83
55
  statePath,
84
56
  state: value,
85
- projectConfig: loadProjectConfig({
86
- projectRoot,
87
- statePath,
88
- state: value
89
- })
57
+ workspaceRoutines: getWorkspaceRoutines({ projectRoot })
90
58
  };
91
59
  }
92
60
 
93
- function resolveHook(config) {
94
- if (typeof config === 'function') {
95
- return config;
96
- }
97
-
98
- if (config && typeof config.initialize === 'function') {
99
- return config.initialize;
100
- }
101
-
102
- return null;
103
- }
104
-
105
61
  module.exports = {
106
62
  buildState,
107
- configFileName,
63
+ getWorkspaceRoutines,
108
64
  initialize,
109
- loadProjectConfig,
110
65
  readEnvironment,
111
66
  resolveProjectRoot,
67
+ routineName,
112
68
  stateDirectoryName,
113
69
  stateFileName
114
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atedone/workspace-context",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Workspace context initialization helper for JavaScript tools.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",