Skip to content

Commit

Permalink
Merge pull request #630 from smapiot/hotfix/#629-dir-entries
Browse files Browse the repository at this point in the history
Test improved pilet pack command
  • Loading branch information
FlorianRappl authored Oct 1, 2023
2 parents 1b6514e + 79097cd commit 05f6c11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/tooling/piral-cli/src/common/pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let json: any = {};
jest.mock('./io', () => ({
readJson: jest.fn(() => json),
removeDirectory: jest.fn(() => Promise.resolve()),
checkIsDirectory: jest.fn(() => false),
makeTempDir: jest.fn(() => Promise.resolve('')),
copy: jest.fn(() => Promise.resolve()),
checkExists: jest.fn(() => Promise.resolve(true)),
Expand Down
48 changes: 30 additions & 18 deletions src/tooling/piral-cli/src/common/pack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { tmpdir } from 'os';
import { resolve, relative, join, dirname, basename } from 'path';
import { resolve, relative, dirname, basename } from 'path';
import { createTgz } from './archive';
import { onlyUniqueFiles } from './utils';
import { log, progress, fail } from './log';
import { readJson, copy, removeDirectory, checkExists, makeTempDir, createDirectory } from './io';
import { readJson, checkExists, createDirectory, checkIsDirectory, getFileNames } from './io';
import { getPossiblePiletMainPaths } from './inspect';

async function getPiletContentDir(root: string, packageData: any) {
Expand All @@ -20,6 +19,26 @@ async function getPiletContentDir(root: string, packageData: any) {
return root;
}

async function getUniqueFiles(files: Array<string>) {
const result: Array<string> = [];

for (const file of files) {
const isDirectory = await checkIsDirectory(file);

if (isDirectory) {
const names = await getFileNames(file);
const subFiles = names.map((name) => resolve(file, name));
const items = await getUniqueFiles(subFiles);
const unique = items.filter((m) => onlyUniqueFiles(m, result.length, result));
result.push(...unique);
} else if (onlyUniqueFiles(file, result.length, result)) {
result.push(file);
}
}

return result;
}

export async function createPiletPackage(baseDir: string, source: string, target: string) {
const root = resolve(baseDir, source);
const dest = resolve(baseDir, target);
Expand Down Expand Up @@ -53,36 +72,29 @@ export async function createPiletPackage(baseDir: string, source: string, target
const readme = resolve(root, 'README.md');

if (Array.isArray(pckg.files)) {
files.push(...pckg.files.map((f) => resolve(root, f)));
const additionalFiles = pckg.files
.filter((f: string) => typeof f === 'string')
.map((f: string) => resolve(root, f));
files.push(...additionalFiles);
}

if (await checkExists(readme)) {
files.push(readme);
}

const prefix = join(tmpdir(), `${id}-`);
const cwd = await makeTempDir(prefix);
const uniqueFiles = files.filter(onlyUniqueFiles);
log('generalDebug_0003', `Creating package with content from "${content}" ...`);

await Promise.all(uniqueFiles.map((file) => copy(file, resolve(cwd, relative(root, file)))));
log('generalDebug_0003', `Reading out unique files from "${content}" ...`);
const uniqueFiles = await getUniqueFiles(files);

log('generalDebug_0003', `Creating directory if not exist for "${file}" ...`);

await createDirectory(dirname(file));

log('generalDebug_0003', `Creating compressed archive at "${file}" relative to "${root}" ...`);

await createTgz(
file,
cwd,
root,
uniqueFiles.map((file) => relative(root, file)),
);

log('generalDebug_0003', `Successfully created package from "${cwd}".`);

await removeDirectory(cwd);

log('generalDebug_0003', `Packed file "${file}".`);
log('generalDebug_0003', `Successfully created package "${file}" from "${root}".`);
return file;
}

0 comments on commit 05f6c11

Please sign in to comment.