diff options
Diffstat (limited to 'scripts/create.mjs')
-rw-r--r-- | scripts/create.mjs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/scripts/create.mjs b/scripts/create.mjs new file mode 100644 index 0000000..096257e --- /dev/null +++ b/scripts/create.mjs | |||
@@ -0,0 +1,93 @@ | |||
1 | /* eslint-disable no-console */ | ||
2 | |||
3 | /** | ||
4 | * Create a new recipe for your service | ||
5 | */ | ||
6 | import fs from 'fs-extra'; | ||
7 | |||
8 | import path from 'path'; | ||
9 | import open from 'open'; | ||
10 | |||
11 | if (process.argv.length < 3) { | ||
12 | console.log(`Usage: pnpm create <Recipe name> [Folder name] | ||
13 | For example: | ||
14 | pnpm create WhatsApp | ||
15 | pnpm create "Google Hangouts" | ||
16 | You can set "Folder name" to "FerdiumDev" to use Ferdium's development instance instead: | ||
17 | |||
18 | pnpm create WhatsApp FerdiumDev | ||
19 | `); | ||
20 | throw new Error('Please provide the correct number of args!'); | ||
21 | } | ||
22 | |||
23 | const recipeName = process.argv[2]; | ||
24 | const recipe = recipeName.toLowerCase().replaceAll(/\s/g, '-'); | ||
25 | const folderName = process.argv[3] || 'Ferdium'; | ||
26 | const filesThatNeedTextReplace = ['package.json', 'index.js', 'webview.js']; | ||
27 | |||
28 | const toPascalCase = str => { | ||
29 | const words = str | ||
30 | .replaceAll(/[^a-z]/g, '') | ||
31 | .split(/\W/) | ||
32 | .map(word => { | ||
33 | if (word.length === 0) { | ||
34 | return word; | ||
35 | } | ||
36 | // Capitalize the first letter, lowercase the rest | ||
37 | return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); | ||
38 | }); | ||
39 | return words.join(''); | ||
40 | }; | ||
41 | const pascalCasedName = toPascalCase(recipe); // PascalCased recipe ID only containing a-z, for usage as the JavaScript class name | ||
42 | |||
43 | (async () => { | ||
44 | // Folder paths | ||
45 | const userData = | ||
46 | process.env.APPDATA || | ||
47 | (process.platform === 'darwin' | ||
48 | ? `${process.env.HOME}/Library/Application Support` | ||
49 | : `${process.env.HOME}/.config`); | ||
50 | const recipesFolder = path.join(userData, folderName, 'recipes'); | ||
51 | const devRecipeFolder = path.join(recipesFolder, 'dev'); | ||
52 | const newRecipeFolder = path.join(devRecipeFolder, recipe); | ||
53 | const sampleRecipe = path.join(import.meta.dirname, 'sample_recipe'); // Starting with Node.js 20.11 / 21.2, you can use import.meta.dirname | ||
54 | |||
55 | // Make sure dev recipe folder exists | ||
56 | if (!fs.existsSync(recipesFolder)) { | ||
57 | console.log( | ||
58 | `Couldn't find your recipe folder (${recipesFolder}). Is Ferdium installed?`, | ||
59 | ); | ||
60 | return; | ||
61 | } | ||
62 | fs.ensureDirSync(devRecipeFolder); | ||
63 | |||
64 | if (fs.existsSync(newRecipeFolder)) { | ||
65 | console.log('⚠️ Recipe already exists'); | ||
66 | return; | ||
67 | } | ||
68 | |||
69 | console.log('[Info] Passed pre-checks'); | ||
70 | |||
71 | // Copy sample recipe to recipe folder | ||
72 | fs.copySync(sampleRecipe, newRecipeFolder); | ||
73 | console.log('[Info] Copied recipe'); | ||
74 | |||
75 | // Replace placeholders with the recipe-specific values | ||
76 | for (const file of filesThatNeedTextReplace) { | ||
77 | const filePath = path.join(newRecipeFolder, file); | ||
78 | let contents = fs.readFileSync(filePath, 'utf8'); | ||
79 | contents = contents.replaceAll('SERVICE', recipe); | ||
80 | contents = contents.replaceAll('SNAME', recipeName); | ||
81 | contents = contents.replaceAll('SPASCAL', pascalCasedName); | ||
82 | fs.writeFileSync(filePath, contents); | ||
83 | } | ||
84 | console.log('[Info] Prepared new recipe'); | ||
85 | |||
86 | open(newRecipeFolder); | ||
87 | console.log(`✅ Successfully created your recipe. | ||
88 | |||
89 | What's next? | ||
90 | - Make sure you restart Ferdium in order for the recipe to show up | ||
91 | - Customise "webview.js", "package.json" and "icon.svg" (see https://github.com/ferdium/ferdium-recipes/blob/main/docs/integration.md#recipe-structure) | ||
92 | - Publish your recipe (see https://github.com/ferdium/ferdium-recipes/blob/main/docs/integration.md#publishing)`); | ||
93 | })(); | ||