@atedone/workspace-context 0.0.0-stage → 0.0.1
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 +4 -2
- package/index.js +12 -0
- package/lib/state.js +53 -0
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @atedone/workspace-context
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Workspace context initialization helper for JavaScript tools.
|
|
4
|
+
|
|
5
|
+
The package initializes `.workspace-context/state.json` when loaded and exposes the same initializer for callers that need to refresh workspace context explicitly.
|
package/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
stateDirectoryName: state.stateDirectoryName,
|
|
11
|
+
stateFileName: state.stateFileName
|
|
12
|
+
};
|
package/lib/state.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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 stateDirectoryName = '.workspace-context';
|
|
9
|
+
const stateFileName = 'state.json';
|
|
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 buildState({ now = () => new Date().toISOString() } = {}) {
|
|
21
|
+
return {
|
|
22
|
+
packageName: manifest.name,
|
|
23
|
+
packageVersion: manifest.version,
|
|
24
|
+
generatedAt: now(),
|
|
25
|
+
mode: 'runtime'
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function initialize({
|
|
30
|
+
projectRoot = resolveProjectRoot(),
|
|
31
|
+
now
|
|
32
|
+
} = {}) {
|
|
33
|
+
const statePath = route.join(projectRoot, stateDirectoryName, stateFileName);
|
|
34
|
+
const value = buildState({ now });
|
|
35
|
+
|
|
36
|
+
file.mkdirSync(route.dirname(statePath), { recursive: true });
|
|
37
|
+
file.writeFileSync(statePath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
projectRoot,
|
|
41
|
+
statePath,
|
|
42
|
+
state: value
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = {
|
|
47
|
+
buildState,
|
|
48
|
+
initialize,
|
|
49
|
+
readEnvironment,
|
|
50
|
+
resolveProjectRoot,
|
|
51
|
+
stateDirectoryName,
|
|
52
|
+
stateFileName
|
|
53
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atedone/workspace-context",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
+
}
|