Skip to content

Commit

Permalink
fix: skip types in current cloud backend (#13803)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk authored May 30, 2024
1 parent 80a5964 commit 81ce57a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/amplify-cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
},
"devDependencies": {
"@aws-amplify/amplify-function-plugin-interface": "1.10.3",
"@types/archiver": "^5.3.1",
"@types/ejs": "^3.1.1",
"@types/fs-extra": "^8.0.1",
"@types/hjson": "^2.4.2",
Expand Down
112 changes: 112 additions & 0 deletions packages/amplify-cli-core/src/__tests__/extractZip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as archiver from 'archiver';
import { extract } from '@aws-amplify/amplify-cli-core';
import * as os from 'os';

describe('extract zip', () => {
let tempDir: string;
let inputDir: string;
let outputDir: string;
let zipFilePath: string;
const file1RelativePath = 'file1.txt';
const file1Content = Math.random().toString();
const file2RelativePath = 'file2.txt';
const file2Content = Math.random().toString();
const dir1RelativePath = 'dir1';
const file3RelativePath = path.join(dir1RelativePath, 'file3.txt');
const file3Content: string = Math.random().toString();
const dir2RelativePath = 'dir2';
const file4RelativePath = path.join(dir2RelativePath, 'file4.txt');
const file4Content: string = Math.random().toString();

beforeAll(async () => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'extractZipTest'));
inputDir = path.join(tempDir, 'inputDir');
outputDir = path.join(tempDir, 'outputDir');
fs.mkdirSync(inputDir);
fs.mkdirSync(path.join(inputDir, 'dir1'));
fs.mkdirSync(path.join(inputDir, 'dir2'));
fs.writeFileSync(path.join(inputDir, file1RelativePath), file1Content);
fs.writeFileSync(path.join(inputDir, file2RelativePath), file2Content);
fs.writeFileSync(path.join(inputDir, file3RelativePath), file3Content);
fs.writeFileSync(path.join(inputDir, file4RelativePath), file4Content);

zipFilePath = path.join(tempDir, 'archive.zip');
const output = fs.createWriteStream(zipFilePath);
const archive = archiver.create('zip', {});
archive.pipe(output);
archive.directory(inputDir, false);
await archive.finalize();
});

beforeEach(() => {
fs.removeSync(outputDir);
});

afterAll(() => {
fs.removeSync(tempDir);
});

it('should extract full zip', async () => {
await extract(zipFilePath, { dir: outputDir });
expect(fs.existsSync(path.join(outputDir, dir1RelativePath))).toStrictEqual(true);
expect(fs.existsSync(path.join(outputDir, dir2RelativePath))).toStrictEqual(true);
expect(fs.readFileSync(path.join(outputDir, file1RelativePath), 'utf-8')).toStrictEqual(file1Content);
expect(fs.readFileSync(path.join(outputDir, file2RelativePath), 'utf-8')).toStrictEqual(file2Content);
expect(fs.readFileSync(path.join(outputDir, file3RelativePath), 'utf-8')).toStrictEqual(file3Content);
expect(fs.readFileSync(path.join(outputDir, file4RelativePath), 'utf-8')).toStrictEqual(file4Content);
});

it('should skip directory', async () => {
await extract(zipFilePath, {
dir: outputDir,
skipEntryPrefixes: [dir1RelativePath],
});
expect(fs.existsSync(path.join(outputDir, dir1RelativePath))).toStrictEqual(false);
expect(fs.existsSync(path.join(outputDir, dir2RelativePath))).toStrictEqual(true);
expect(fs.readFileSync(path.join(outputDir, file1RelativePath), 'utf-8')).toStrictEqual(file1Content);
expect(fs.readFileSync(path.join(outputDir, file2RelativePath), 'utf-8')).toStrictEqual(file2Content);
expect(fs.existsSync(path.join(outputDir, file3RelativePath))).toStrictEqual(false);
expect(fs.readFileSync(path.join(outputDir, file4RelativePath), 'utf-8')).toStrictEqual(file4Content);
});

it('should skip top level file', async () => {
await extract(zipFilePath, {
dir: outputDir,
skipEntryPrefixes: [file1RelativePath],
});
expect(fs.existsSync(path.join(outputDir, dir1RelativePath))).toStrictEqual(true);
expect(fs.existsSync(path.join(outputDir, dir2RelativePath))).toStrictEqual(true);
expect(fs.existsSync(path.join(outputDir, file1RelativePath))).toStrictEqual(false);
expect(fs.readFileSync(path.join(outputDir, file2RelativePath), 'utf-8')).toStrictEqual(file2Content);
expect(fs.readFileSync(path.join(outputDir, file3RelativePath), 'utf-8')).toStrictEqual(file3Content);
expect(fs.readFileSync(path.join(outputDir, file4RelativePath), 'utf-8')).toStrictEqual(file4Content);
});

it('should skip nested file', async () => {
await extract(zipFilePath, {
dir: outputDir,
skipEntryPrefixes: [file4RelativePath],
});
expect(fs.existsSync(path.join(outputDir, dir1RelativePath))).toStrictEqual(true);
expect(fs.existsSync(path.join(outputDir, dir2RelativePath))).toStrictEqual(true);
expect(fs.readFileSync(path.join(outputDir, file1RelativePath), 'utf-8')).toStrictEqual(file1Content);
expect(fs.readFileSync(path.join(outputDir, file2RelativePath), 'utf-8')).toStrictEqual(file2Content);
expect(fs.readFileSync(path.join(outputDir, file3RelativePath), 'utf-8')).toStrictEqual(file3Content);
expect(fs.existsSync(path.join(outputDir, file4RelativePath))).toStrictEqual(false);
});

it('should skip multiple entries', async () => {
await extract(zipFilePath, {
dir: outputDir,
skipEntryPrefixes: [file1RelativePath, dir2RelativePath],
});
expect(fs.existsSync(path.join(outputDir, dir1RelativePath))).toStrictEqual(true);
expect(fs.existsSync(path.join(outputDir, dir2RelativePath))).toStrictEqual(false);
expect(fs.existsSync(path.join(outputDir, file1RelativePath))).toStrictEqual(false);
expect(fs.readFileSync(path.join(outputDir, file2RelativePath), 'utf-8')).toStrictEqual(file2Content);
expect(fs.readFileSync(path.join(outputDir, file3RelativePath), 'utf-8')).toStrictEqual(file3Content);
expect(fs.existsSync(path.join(outputDir, file4RelativePath))).toStrictEqual(false);
});
});
9 changes: 9 additions & 0 deletions packages/amplify-cli-core/src/extractZip.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class Extractor {
return;
}

if (this.opts.skipEntryPrefixes && Array.isArray(this.opts.skipEntryPrefixes) && this.opts.skipEntryPrefixes.length > 0) {
for (const skipEntriesPrefix of this.opts.skipEntryPrefixes) {
if (entry.fileName.startsWith(skipEntriesPrefix)) {
this.zipfile.readEntry();
return;
}
}
}

const destDir = path.dirname(path.join(this.opts.dir, entry.fileName));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ async function downloadBackend(context, backendEnv, awsConfigInfo) {

const unzippedDirPath = path.join(tempDirPath, path.basename(zipFileName, '.zip'));

await extract(tempFilePath, { dir: unzippedDirPath });
await extract(tempFilePath, { dir: unzippedDirPath, skipEntryPrefixes: ['types/'] });

// Move out cli.*json if exists in the temp directory into the amplify directory before copying backend and
// current cloud backend directories.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const archiver = require('archiver');
const path = require('path');
const fs = require('fs-extra');

const DEFAULT_IGNORE_PATTERN = ['*/*/build/**', '*/*/dist/**', 'function/*/src/node_modules/**'];
const DEFAULT_IGNORE_PATTERN = ['*/*/build/**', '*/*/dist/**', 'function/*/src/node_modules/**', 'types/**'];

async function run(folder, zipFilePath, ignorePattern = DEFAULT_IGNORE_PATTERN, extraFiles) {
const zipFileFolder = path.dirname(zipFilePath);
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ __metadata:
"@aws-amplify/amplify-prompts": 2.8.6
"@aws-amplify/graphql-transformer-interfaces": ^3.7.0
"@aws-sdk/util-arn-parser": ^3.310.0
"@types/archiver": ^5.3.1
"@types/ejs": ^3.1.1
"@types/fs-extra": ^8.0.1
"@types/hjson": ^2.4.2
Expand Down

0 comments on commit 81ce57a

Please sign in to comment.