Skip to content

Commit 971b144

Browse files
committed
port setup scripts from powershell to Node
1 parent 6962947 commit 971b144

7 files changed

Lines changed: 382 additions & 294 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"start": "node --experimental-transform-types src/start.ts",
1010
"devices": "node --experimental-transform-types src/start.ts --devices-only",
1111
"ctt": "ctt/bin/CTT-Remote.exe ctt/project/zwave-js.cttsln",
12-
"create-setup-archive": "powershell -ExecutionPolicy Bypass -File setup/create-setup-archive.ps1",
12+
"create-setup-archive": "node --experimental-transform-types setup/create-setup-archive.ts",
13+
"extract-setup-archive": "node --experimental-transform-types setup/extract-setup-archive.ts",
1314
"setup": "node --experimental-transform-types setup/download-zwave-stack.ts"
1415
},
1516
"keywords": [],

setup/create-setup-archive.ps1

Lines changed: 0 additions & 115 deletions
This file was deleted.

setup/create-setup-archive.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* Creates a setup archive containing all files needed for CTT tests on CI.
3+
*
4+
* This script packages the following into setup/setup.zip:
5+
* - zwave_stack/storage/ -> storage/
6+
* - DUT storage files (from config.json glob patterns) -> dut-storage/
7+
* - CTT AppData folder -> appdata/
8+
* - CTT Keys file (homeId-specific) -> keys/
9+
*
10+
* Usage: node --experimental-transform-types setup/create-setup-archive.ts
11+
*/
12+
13+
import { execSync } from "node:child_process";
14+
import {
15+
copyFileSync,
16+
cpSync,
17+
existsSync,
18+
mkdirSync,
19+
readdirSync,
20+
readFileSync,
21+
rmSync,
22+
unlinkSync,
23+
} from "node:fs";
24+
import { tmpdir, homedir } from "node:os";
25+
import { basename, dirname, join } from "node:path";
26+
import { fileURLToPath } from "node:url";
27+
import JSON5 from "json5";
28+
import c from "ansi-colors";
29+
30+
const __filename = fileURLToPath(import.meta.url);
31+
const __dirname = dirname(__filename);
32+
33+
const repoRoot = dirname(__dirname);
34+
const tempDir = join(tmpdir(), "ctt-setup-staging");
35+
const outputFile = join(repoRoot, "setup", "setup.zip");
36+
37+
interface Config {
38+
dut: {
39+
homeId: string;
40+
storageDir: string;
41+
storageFileFilter: string[];
42+
};
43+
}
44+
45+
// Load config.json (supports JSON5 comments)
46+
const configPath = join(repoRoot, "config.json");
47+
const config: Config = JSON5.parse(readFileSync(configPath, "utf-8"));
48+
49+
// DUT configuration
50+
const homeId = config.dut.homeId;
51+
const homeIdLower = homeId.toLowerCase();
52+
const homeIdUpper = homeId.toUpperCase();
53+
const dutStorageDir = join(repoRoot, config.dut.storageDir);
54+
const dutStorageArchiveName = "dut-storage";
55+
56+
// Source paths
57+
const zwaveStorage = join(repoRoot, "zwave_stack", "storage");
58+
const username = process.env.USERNAME || basename(homedir());
59+
const cttAppData = join(
60+
"C:",
61+
"Users",
62+
username,
63+
"AppData",
64+
"Roaming",
65+
"Z-Wave Alliance",
66+
"Z-Wave CTT 3",
67+
);
68+
const cttKeys = join(
69+
"C:",
70+
"Users",
71+
username,
72+
"Documents",
73+
"Z-Wave Alliance",
74+
"Z-Wave CTT 3",
75+
"Keys",
76+
);
77+
78+
console.log(c.cyan("Creating setup archive..."));
79+
80+
// Clean up any existing temp directory
81+
if (existsSync(tempDir)) {
82+
rmSync(tempDir, { recursive: true, force: true });
83+
}
84+
85+
// Create staging directory
86+
mkdirSync(tempDir, { recursive: true });
87+
88+
// Copy zwave_stack/storage
89+
if (existsSync(zwaveStorage)) {
90+
cpSync(zwaveStorage, join(tempDir, "storage"), { recursive: true });
91+
}
92+
93+
// Copy DUT storage files using glob patterns from config
94+
const dutStagingDir = join(tempDir, dutStorageArchiveName);
95+
mkdirSync(dutStagingDir, { recursive: true });
96+
97+
if (existsSync(dutStorageDir)) {
98+
for (const pattern of config.dut.storageFileFilter) {
99+
// Replace placeholders
100+
const resolvedPattern = pattern
101+
.replace(/%HOME_ID_LOWER%/g, homeIdLower)
102+
.replace(/%HOME_ID_UPPER%/g, homeIdUpper);
103+
104+
// Simple glob matching - for now just match files in the directory
105+
const files = readdirSync(dutStorageDir);
106+
const regex = new RegExp(
107+
"^" +
108+
resolvedPattern
109+
.replace(/\./g, "\\.")
110+
.replace(/\*/g, ".*")
111+
.replace(/\?/g, ".") +
112+
"$",
113+
);
114+
115+
for (const file of files) {
116+
if (regex.test(file)) {
117+
copyFileSync(
118+
join(dutStorageDir, file),
119+
join(dutStagingDir, file),
120+
);
121+
}
122+
}
123+
}
124+
}
125+
126+
// Copy CTT AppData
127+
if (existsSync(cttAppData)) {
128+
cpSync(cttAppData, join(tempDir, "appdata"), { recursive: true });
129+
}
130+
131+
// Copy CTT Keys - only the homeId-specific key file
132+
const keysStagingDir = join(tempDir, "keys");
133+
mkdirSync(keysStagingDir, { recursive: true });
134+
135+
if (existsSync(cttKeys)) {
136+
const keyFile = join(cttKeys, `${homeIdUpper}.txt`);
137+
if (existsSync(keyFile)) {
138+
copyFileSync(keyFile, join(keysStagingDir, `${homeIdUpper}.txt`));
139+
}
140+
}
141+
142+
// Remove old archive if it exists
143+
if (existsSync(outputFile)) {
144+
unlinkSync(outputFile);
145+
}
146+
147+
// Create the zip archive
148+
const archiveContents = readdirSync(tempDir).join(" ");
149+
if (process.platform === "win32") {
150+
// Use Windows tar.exe which supports -a for zip format
151+
execSync(
152+
`tar.exe --force-local -a -cf "${outputFile}" -C "${tempDir}" ${archiveContents}`,
153+
{ stdio: "inherit" },
154+
);
155+
} else {
156+
execSync(`cd "${tempDir}" && zip -r "${outputFile}" .`, {
157+
stdio: "inherit",
158+
shell: "/bin/bash",
159+
});
160+
}
161+
162+
// Clean up temp directory
163+
rmSync(tempDir, { recursive: true, force: true });
164+
165+
console.log(c.green(`Created ${outputFile}`));

setup/download-zwave-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ try {
3232
}
3333

3434
console.log(`Extracting ${tarball}...`);
35-
execSync(`tar --force-local -xzf "${join(tempDir, tarball)}" -C "${tempDir}"`, {
35+
execSync(`tar.exe --force-local -xzf "${join(tempDir, tarball)}" -C "${tempDir}"`, {
3636
stdio: "inherit",
3737
});
3838

0 commit comments

Comments
 (0)