-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathnew-actor.mjs
executable file
·41 lines (36 loc) · 1.47 KB
/
new-actor.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import url from "url";
import { Command } from "commander/esm.mjs";
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const actorsDir = path.resolve(__dirname, "..", "actors");
const scaffoldDir = path.resolve(__dirname, "scaffolding", "actor");
async function main(actorName, actorApifyName = actorName) {
if (!actorName) {
console.error("Please provide an actor name");
process.exit(1);
}
const actorDir = path.join(actorsDir, actorName);
if (fs.existsSync(actorDir)) {
console.error(`Directory for ${actorName} actor already exists, aborting`);
process.exit(1);
}
await fs.promises.mkdir(actorDir, { recursive: true });
const files = await fs.promises.readdir(scaffoldDir);
for (const file of files) {
const filePath = path.join(scaffoldDir, file);
const fileContent = await fs.promises.readFile(filePath, "utf8");
const newFileContent = fileContent
.replace(/ACTOR_NAME/g, actorName)
.replace(/ACTOR_APIFY_NAME/g, actorApifyName)
.replace(/ACTOR_PACKAGE_NAME/g, `@hlidac-shopu/${actorName}`);
const newFilePath = path.join(actorDir, file);
await fs.promises.writeFile(newFilePath, newFileContent, "utf8");
}
console.log(`New actor "${actorName}" prepared. You can start coding now:\n${actorDir}/main.js`);
}
const program = new Command("createActor")
.arguments("<actor-name> [actor-apify-name]")
.action(main)
.parse(process.argv);