Skip to content

Commit

Permalink
Individual packing for more flexibility and lighter packages
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jul 7, 2022
1 parent 34be0ca commit a850ff7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Improved default metadata using `piletConfig` from *package.json* in `pilet debug` (#458)
- Updated to React v18 (#501)
- Updated `importmap` with `inherit` key
- Moved templates to dedicated repository (#458)
- Changed the default bundler to be `piral-cli-webpack5` (#469)
- Changed webpack configs to use `oneOf` for assets (#451)
Expand Down
18 changes: 8 additions & 10 deletions src/tooling/piral-cli/src/common/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ function getPackageJson(files: PackageFiles): PackageData {
return jju.parse(content);
}

function getPiletMainPath(data: PackageData, files: PackageFiles) {
const paths = [
data.main,
`dist/${data.main}`,
`${data.main}/index.js`,
`dist/${data.main}/index.js`,
'index.js',
'dist/index.js',
];
return paths.map((filePath) => `${packageRoot}${filePath}`).filter((filePath) => !!files[filePath])[0];
export function getPossiblePiletMainPaths(data: PackageData) {
const { main = 'index.js' } = data;
return [main, `dist/${main}`, `${main}/index.js`, `dist/${main}/index.js`, 'index.js', 'dist/index.js'];
}

export function getPiletMainPath(data: PackageData, files: PackageFiles) {
const paths = getPossiblePiletMainPaths(data);
return paths.map((filePath) => `${packageRoot}${filePath}`).find((filePath) => !!files[filePath]);
}

export interface PiletPackageData extends PackageData {
Expand Down
98 changes: 78 additions & 20 deletions src/tooling/piral-cli/src/common/pack.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
import { resolve, join } from 'path';
import { tmpdir } from 'os';
import { resolve, relative, join, dirname } from 'path';
import { createWriteStream, mkdtemp } from 'fs';
import { log, progress, fail } from './log';
import { readJson, move } from './io';
import { createNpmPackage } from './npm';
import { ForceOverwrite } from './enums';

async function getFile(root: string, name: string, dest: string) {
const proposed = join(root, name);

if (dest !== root) {
log('generalDebug_0003', `Moving file from "${root}" to "${dest}" ...`);
const file = await move(proposed, dest, ForceOverwrite.yes);
log('generalDebug_0003', 'Successfully moved file.');
return file;
import { readJson, copy, removeDirectory, checkExists } from './io';
import { getPossiblePiletMainPaths } from './inspect';
import { tar } from '../external';

async function getPiletContentDir(root: string, packageData: any) {
const paths = getPossiblePiletMainPaths(packageData);

for (const path of paths) {
const file = resolve(root, path);

if (await checkExists(file)) {
return dirname(file);
}
}

return proposed;
return root;
}

export function makeTempDir(prefix: string) {
return new Promise<string>((resolve, reject) =>
mkdtemp(prefix, (err, folder) => {
if (err) {
reject(err);
} else {
resolve(folder);
}
}),
);
}

export function packContent(file: string, cwd: string, files: Array<string>) {
const stream = createWriteStream(file);

tar
.c(
{
cwd,
prefix: 'package/',
portable: true,
follow: true,
gzip: {
level: 9,
},
// @ts-ignore
mtime: new Date('1985-10-26T08:15:00.000Z'),
},
files,
)
.pipe(stream, {
end: true,
});

return new Promise((finish) => stream.on('close', finish));
}

export async function createPiletPackage(baseDir: string, source: string, target: string) {
Expand All @@ -37,12 +77,30 @@ export async function createPiletPackage(baseDir: string, source: string, target
}

progress(`Packing pilet in ${dest} ...`);
log('generalDebug_0003', 'Creating package ...');
await createNpmPackage(root);
log('generalDebug_0003', 'Successfully created package.');
const name = `${pckg.name}-${pckg.version}.tgz`.replace(/@/g, '').replace(/\//g, '-');
log('generalDebug_0003', `Assumed package name "${name}".`);
const file = await getFile(root, name, dest);
const pckgName = pckg.name.replace(/@/g, '').replace(/\//g, '-');
const id = `${pckgName}-${pckg.version}`;
const name = `${id}.tgz`;
log('generalDebug_0003', `Assume package name "${name}".`);

const file = resolve(dest, name);
const content = await getPiletContentDir(root, pckg);
const files = [resolve(root, 'package.json'), content];
const prefix = join(tmpdir(), `${id}-`);
const cwd = await makeTempDir(prefix);
log('generalDebug_0003', `Creating package with content from "${content}" ...`);

await Promise.all(files.map((file) => copy(file, resolve(cwd, relative(root, file)))));

await packContent(
file,
cwd,
files.map((f) => relative(root, f)),
);

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

await removeDirectory(cwd);

log('generalDebug_0003', `Packed file "${file}".`);
return file;
}
20 changes: 4 additions & 16 deletions src/tooling/piral-cli/src/common/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,21 +518,7 @@ export async function patchPiletPackage(
...info.scripts,
}
: info.scripts;
const peerModules = [];
const allExternals = makePiletExternals(root, piralDependencies, externals, fromEmulator, piralInfo);
const peerDependencies = {
...allExternals.reduce((deps, name) => {
const valid = isValidDependency(name);
deps[name] = valid ? '*' : undefined;

if (!valid) {
peerModules.push(name);
}

return deps;
}, {}),
[name]: `*`,
};
const devDependencies: Record<string, string> = {
...Object.keys(typeDependencies).reduce((deps, name) => {
deps[name] = piralDependencies[name] || typeDependencies[name];
Expand Down Expand Up @@ -564,9 +550,11 @@ export async function patchPiletPackage(

const packageContent = deepMerge(packageOverrides, {
piral,
importmap: {
imports: {},
inherit: [name],
},
devDependencies,
peerDependencies,
peerModules,
dependencies: {
[name]: undefined,
},
Expand Down

0 comments on commit a850ff7

Please sign in to comment.