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-04 03:20:55 -06:00
|
|
|
export default async (name, source, type, flecks) => {
|
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];
|
|
|
|
});
|
|
|
|
// 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());
|
|
|
|
});
|
2024-01-04 03:20:55 -06:00
|
|
|
return fileTree;
|
2022-03-01 01:52:56 -06:00
|
|
|
};
|