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

50 lines
1.1 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
2024-01-04 03:20:55 -06:00
import {
JsonStream,
transform,
} from '@flecks/core/server';
2022-03-01 01:52:56 -06:00
import FileTree from './tree';
2024-01-04 03:20:55 -06:00
export const 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-13 08:37:38 -06:00
export default 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();
}),
);
// Pretty print all JSON.
fileTree.glob('**/*.json')
.forEach((path) => {
fileTree.pipe(path, new JsonStream.PrettyPrint());
});
2024-01-04 03:20:55 -06:00
return fileTree;
2022-03-01 01:52:56 -06:00
};