Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail Feature/Template packaging if id does not match containing directory name #797

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/spec-node/collectionCommonUtils/packageCommandImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,39 +183,44 @@ export async function packageCollection(args: PackageCommandInput, collectionTyp
// Validate minimal folder structure
const devcontainerJsonName = `devcontainer-${collectionType}.json`;

// Be tolerant of other folders that may not contain Feature source code
if (!(await isLocalFile(path.join(folder, devcontainerJsonName)))) {
output.write(`(!) WARNING: ${collectionType} '${c}' is missing a ${devcontainerJsonName}. Skipping... `, LogLevel.Warning);
output.write(`(!) WARNING: ${collectionType} '${c}' is missing a ${devcontainerJsonName}. Skipping...`, LogLevel.Warning);
continue;
}

const tmpSrcDir = path.join(os.tmpdir(), `/templates-src-output-${Date.now()}`);
await cpDirectoryLocal(folder, tmpSrcDir);
const jsonPath = path.join(tmpSrcDir, devcontainerJsonName);
const metadata = jsonc.parse(await readLocalFile(jsonPath, 'utf-8'));

const archiveName = getArchiveName(c, collectionType);
if (!metadata.id || !metadata.version || !metadata.name) {
output.write(`ERROR: ${collectionType} '${c}' is missing one of the following required properties in its ${devcontainerJsonName}: 'id', 'version', 'name'.`, LogLevel.Error);
return;
}

const jsonPath = path.join(tmpSrcDir, devcontainerJsonName);
// Validate that the 'id' field in the metadata property matches the folder name
if (c !== metadata.id) {
output.write(`ERROR: ${collectionType} id '${metadata.id}' does not match its containing folder name '${c}'.`, LogLevel.Error);
return;
}

if (collectionType === 'feature') {
const installShPath = path.join(tmpSrcDir, 'install.sh');
if (!(await isLocalFile(installShPath))) {
output.write(`Feature '${c}' is missing an install.sh`, LogLevel.Error);
return;
}

await addsAdditionalFeatureProps(jsonPath, output);
} else if (collectionType === 'template') {
if (!(await addsAdditionalTemplateProps(tmpSrcDir, jsonPath, output))) {
return;
}
}

const archiveName = getArchiveName(c, collectionType);
await tarDirectory(tmpSrcDir, archiveName, outputDir);

const metadata = jsonc.parse(await readLocalFile(jsonPath, 'utf-8'));
if (!metadata.id || !metadata.version || !metadata.name) {
output.write(`${collectionType} '${c}' is missing one of the following required properties in its ${devcontainerJsonName}: 'id', 'version', 'name'.`, LogLevel.Error);
return;
}
metadatas.push(metadata);
await rmLocal(tmpSrcDir, { recursive: true, force: true });
}
Expand Down
6 changes: 3 additions & 3 deletions src/spec-node/featuresCLI/packageCommandImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export async function doFeaturesPackageCommand(args: PackageCommandInput): Promi
let metadataOutput: Feature[] | undefined = [];
if (isSingleFeature) {
// Package individual features
output.write('Packaging single feature...', LogLevel.Info);
output.write('Packaging single Feature...', LogLevel.Info);
metadataOutput = await packageSingleFeature(args);
} else {
output.write('Packaging feature collection...', LogLevel.Info);
output.write('Packaging Feature collection...', LogLevel.Info);
metadataOutput = await packageFeatureCollection(args);
}

if (!metadataOutput) {
output.write('Failed to package features', LogLevel.Error);
output.write('Failed to package Features', LogLevel.Error);
return undefined;
}

Expand Down
Loading