MCPcopy
hub / github.com/browserless/browserless / create

Function create

bin/browserless.js:370–416  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

368};
369
370const create = async () => {
371 const validNameRegex = /^[a-zA-Z0-9-_]+$/gi;
372 const directory = (
373 await prompt('What should we name this project (hyphens are ok)?')
374 ).trim();
375 const scaffoldLocation = path.join(__dirname, 'scaffold');
376
377 if (!directory) {
378 throw new Error(`A valid name is required.`);
379 }
380
381 const isValidDirectory = validNameRegex.test(directory);
382
383 if (!isValidDirectory) {
384 throw new Error(`Name must not include special characters.`);
385 }
386
387 const installPath = path.join(projectDir, directory);
388 log(`Creating folder "${installPath}"...`);
389 await fs.mkdir(installPath);
390
391 log(`Copying Project Dependencies...`);
392 const sdkFiles = await fs.readdir(scaffoldLocation, { recursive: true });
393 for (const sdkFile of sdkFiles) {
394 const from = path.join(scaffoldLocation, sdkFile);
395 const to = path.join(installPath, sdkFile);
396 if (sdkFile === 'package.json') {
397 const sdkPackageJSONTemplate = (await readFile(from)).toString();
398 const { version } = await browserlessPackageJSON;
399 const sdkPackageJSON = sdkPackageJSONTemplate
400 .replace('${BROWSERLESS_VERSION}', version)
401 .replace('"name": ""', `"name": "${directory}"`);
402 await writeFile(to, sdkPackageJSON);
403 } else if ((await fs.lstat(from)).isDirectory()) {
404 await fs.mkdir(to);
405 } else {
406 await fs.copyFile(from, to);
407 }
408 }
409
410 log(`Installing browsers and npm modules, this might take a few minutes...`);
411 await installDependencies(installPath);
412
413 log(
414 `Done! You can now open "${installPath}" in an editor of your choice. Make sure to check out the README and update the package.json file!`,
415 );
416};
417
418const help = async () => {
419 console.log(`Version: ${(await browserlessPackageJSON).version}`);

Callers 1

browserless.jsFile · 0.85

Calls 3

promptFunction · 0.85
readFileFunction · 0.85
installDependenciesFunction · 0.85

Tested by

no test coverage detected