flecks/packages/create-app/src/move.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-03-01 01:52:56 -06:00
import {
stat,
} from 'fs/promises';
2024-01-02 16:10:34 -06:00
import {basename, dirname, join} from 'path';
2022-03-01 01:52:56 -06:00
import {JsonStream, transform} from '@flecks/core/server';
import FileTree from './tree';
const testDestination = async (destination) => {
try {
await stat(destination);
return false;
}
catch (error) {
if ('ENOENT' !== error.code) {
throw error;
}
return true;
}
};
2022-03-01 10:30:33 -06:00
export default async (name, source, destination, type, flecks) => {
2022-03-01 01:52:56 -06:00
if (!await testDestination(destination)) {
const error = new Error(
`@flecks/create-fleck: destination '${destination} already exists: aborting`,
);
error.code = 129;
throw error;
}
const fileTree = await FileTree.loadFrom(source);
// Renamed to avoid conflicts.
const {files} = fileTree;
fileTree.glob('**/*.noconflict')
.forEach((path) => {
2024-01-02 16:10:34 -06:00
files[join(dirname(path), basename(path, '.noconflict'))] = files[path];
2022-03-01 01:52:56 -06:00
delete files[path];
});
// Defaults.
2022-03-01 10:30:33 -06:00
flecks.set(
`@flecks/create-${type}.packager`,
flecks.get(`@flecks/create-${type}.packager`, ['...']),
);
2022-03-01 01:52:56 -06:00
// Send it out.
2022-03-01 10:30:33 -06:00
await flecks.invokeSequentialAsync(`@flecks/create-${type}/packager`, fileTree);
2022-03-01 01:52:56 -06:00
// Add project name to `package.json`.
fileTree.pipe(
'package.json',
transform((chunk, encoding, done, stream) => {
stream.push(JSON.stringify({name, ...JSON.parse(chunk)}));
done();
}),
);
// Pretty print all JSON.
fileTree.glob('**/*.json')
.forEach((path) => {
fileTree.pipe(path, new JsonStream.PrettyPrint());
});
// Write the tree.
await fileTree.writeTo(destination);
};