Skip to content

Commit 468424d

Browse files
authored
[Dev tool] Support samples v2 directory structure in check node versions command (Azure#15328)
Samples v2 uses a directory structure like this `samples/v<some package version>` instead of the good old `samples`. Furthermore, it is not guaranteed that the package version is the most recent major one because a package author can decide to change it to another number. This PR adds logic to handle both samples v1 and v2 and make sure to use the appropriate corresponding path. If there is more than one samples v2 directory, the command will run the samples in the directory with the largest version number.
1 parent 4f2c6cb commit 468424d

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

common/tools/dev-tool/src/commands/samples/checkNodeVersions.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,40 @@ async function cleanup(
6868
await deleteDockerImages(dockerImageNames);
6969
}
7070

71+
function findSamplesDir(samplesDir: string, rootDir: string): string {
72+
const dirs = [];
73+
for (const file of fs.readdirSync(samplesDir)) {
74+
const stats = fs.statSync(path.join(samplesDir, file));
75+
if (stats.isDirectory()) {
76+
if (file.match(/^v[0-9]*.*$/)) {
77+
dirs.push(file);
78+
}
79+
}
80+
}
81+
if (dirs.length === 0) {
82+
return `${rootDir}/samples`;
83+
} else {
84+
return `${rootDir}/samples/${dirs
85+
.sort()
86+
.slice(-1)
87+
.pop()}`;
88+
}
89+
}
7190
function buildRunSamplesScript(
7291
containerWorkspacePath: string,
92+
samplesPath: string,
7393
artifactURL: string,
7494
envFileName: string,
7595
logFilePath?: string
7696
) {
7797
function compileCMD(cmd: string, printToScreen?: boolean) {
7898
return printToScreen ? cmd : `${cmd} >> ${logFilePath} 2>&1`;
7999
}
100+
const samplesDir = findSamplesDir(samplesPath, containerWorkspacePath);
80101
const printToScreen = logFilePath === undefined;
81102
const envFilePath = `${containerWorkspacePath}/${envFileName}`;
82-
const javascriptSamplesPath = `${containerWorkspacePath}/samples/javascript`;
83-
const typescriptCompiledSamplesPath = `${containerWorkspacePath}/samples/typescript/dist`;
103+
const javascriptSamplesPath = `${samplesDir}/javascript`;
104+
const typescriptCompiledSamplesPath = `${samplesDir}/typescript/dist`;
84105
const scriptContent = `#!/bin/sh
85106
86107
function install_dependencies_helper() {
@@ -92,9 +113,9 @@ function install_dependencies_helper() {
92113
93114
function install_packages() {
94115
echo "Using node \$(node -v) to install dependencies";
95-
install_dependencies_helper ${containerWorkspacePath}/samples/javascript
96-
install_dependencies_helper ${containerWorkspacePath}/samples/typescript;
97-
cp ${envFilePath} ${containerWorkspacePath}/samples/javascript/;
116+
install_dependencies_helper ${samplesDir}/javascript
117+
install_dependencies_helper ${samplesDir}/typescript;
118+
cp ${envFilePath} ${samplesDir}/javascript/;
98119
}
99120
100121
function run_samples() {
@@ -108,9 +129,9 @@ function run_samples() {
108129
109130
function build_typescript() {
110131
echo "Using node \$(node -v) to build the typescript samples";
111-
cd ${containerWorkspacePath}/samples/typescript
132+
cd ${samplesDir}/typescript
112133
${compileCMD(`npm run build`, printToScreen)}
113-
cp ${envFilePath} ${containerWorkspacePath}/samples/typescript/dist/
134+
cp ${envFilePath} ${samplesDir}/typescript/dist/
114135
}
115136
116137
function main() {
@@ -126,7 +147,7 @@ main`;
126147
function createDockerContextDirectory(
127148
dockerContextDirectory: string,
128149
containerWorkspacePath: string,
129-
samples_path: string,
150+
samplesPath: string,
130151
envPath: string,
131152
artifactPath?: string,
132153
logFilePath?: string
@@ -143,7 +164,7 @@ function createDockerContextDirectory(
143164
throw new Error("artifact_path is a required argument but it was not passed");
144165
}
145166
const envFileName = path.basename(envPath);
146-
fs.copySync(samples_path, path.join(dockerContextDirectory, "samples"));
167+
fs.copySync(samplesPath, path.join(dockerContextDirectory, "samples"));
147168
let artifactURL: string | undefined = undefined;
148169
if (fs.existsSync(artifactPath)) {
149170
const artifactName = path.basename(artifactPath);
@@ -157,7 +178,13 @@ function createDockerContextDirectory(
157178
fs.copyFileSync(envPath, path.join(dockerContextDirectory, envFileName));
158179
fs.writeFileSync(
159180
path.join(dockerContextDirectory, "run_samples.sh"),
160-
buildRunSamplesScript(containerWorkspacePath, artifactURL, envFileName, logFilePath),
181+
buildRunSamplesScript(
182+
containerWorkspacePath,
183+
samplesPath,
184+
artifactURL,
185+
envFileName,
186+
logFilePath
187+
),
161188
{ mode: S_IRWXO }
162189
);
163190
}

0 commit comments

Comments
 (0)