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

40 lines
964 B
JavaScript
Raw Normal View History

2024-01-20 04:59:13 -06:00
const {stat} = require('fs/promises');
const {basename, dirname, join} = require('path');
2022-03-01 01:52:56 -06:00
2024-01-21 06:56:53 -06:00
const {transform} = require('@flecks/core/server');
2022-03-01 01:52:56 -06:00
2024-01-20 04:59:13 -06:00
const FileTree = require('./tree');
2022-03-01 01:52:56 -06:00
2024-01-20 04:59:13 -06:00
exports.testDestination = async (destination) => {
2022-03-01 01:52:56 -06:00
try {
await stat(destination);
return false;
}
catch (error) {
if ('ENOENT' !== error.code) {
throw error;
}
return true;
}
};
2024-01-20 04:59:13 -06:00
exports.move = async (name, source) => {
2022-03-01 01:52:56 -06:00
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];
});
// Add project name to `package.json`.
fileTree.pipe(
'package.json',
transform((chunk, encoding, done, stream) => {
stream.push(JSON.stringify({name, ...JSON.parse(chunk)}));
done();
}),
);
2024-01-04 03:20:55 -06:00
return fileTree;
2022-03-01 01:52:56 -06:00
};