@atedone/workspace-context 0.0.0-stage → 0.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/README.md CHANGED
@@ -1,3 +1,5 @@
1
- # Temporary Holding Version
1
+ # @atedone/workspace-context
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Workspace context initialization helper for JavaScript tools.
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.
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ 'use strict';
2
+
3
+ const state = require('./lib/state');
4
+
5
+ const initialized = state.initialize();
6
+
7
+ module.exports = {
8
+ initialized,
9
+ initialize: state.initialize,
10
+ configFileName: state.configFileName,
11
+ stateDirectoryName: state.stateDirectoryName,
12
+ stateFileName: state.stateFileName
13
+ };
package/lib/state.js ADDED
@@ -0,0 +1,114 @@
1
+ 'use strict';
2
+
3
+ const platform = process;
4
+ const file = platform.getBuiltinModule('fs');
5
+ const route = platform.getBuiltinModule('path');
6
+ const runtime = platform.getBuiltinModule('module');
7
+ const manifest = require('../package.json');
8
+
9
+ const configFileName = 'workspace.context.js';
10
+ const stateDirectoryName = '.workspace-context';
11
+ const stateFileName = 'state.json';
12
+
13
+ function readEnvironment() {
14
+ return Reflect.get(platform, 'env') || {};
15
+ }
16
+
17
+ function resolveProjectRoot(env = readEnvironment(), cwd = platform.cwd()) {
18
+ const initCwd = typeof env.INIT_CWD === 'string' ? env.INIT_CWD.trim() : '';
19
+ return initCwd || cwd;
20
+ }
21
+
22
+ function buildState({ now = () => new Date().toISOString() } = {}) {
23
+ return {
24
+ packageName: manifest.name,
25
+ packageVersion: manifest.version,
26
+ generatedAt: now(),
27
+ mode: 'runtime-config'
28
+ };
29
+ }
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
+ };
69
+ }
70
+
71
+ function initialize({
72
+ projectRoot = resolveProjectRoot(),
73
+ now
74
+ } = {}) {
75
+ const statePath = route.join(projectRoot, stateDirectoryName, stateFileName);
76
+ const value = buildState({ now });
77
+
78
+ file.mkdirSync(route.dirname(statePath), { recursive: true });
79
+ file.writeFileSync(statePath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
80
+
81
+ return {
82
+ projectRoot,
83
+ statePath,
84
+ state: value,
85
+ projectConfig: loadProjectConfig({
86
+ projectRoot,
87
+ statePath,
88
+ state: value
89
+ })
90
+ };
91
+ }
92
+
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
+ module.exports = {
106
+ buildState,
107
+ configFileName,
108
+ initialize,
109
+ loadProjectConfig,
110
+ readEnvironment,
111
+ resolveProjectRoot,
112
+ stateDirectoryName,
113
+ stateFileName
114
+ };
package/package.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
2
  "name": "@atedone/workspace-context",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.2",
4
+ "description": "Workspace context initialization helper for JavaScript tools.",
5
+ "main": "index.js",
6
+ "type": "commonjs",
7
+ "files": [
8
+ "lib",
9
+ "index.js",
10
+ "README.md"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "atedone"
14
+ }