aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/create.mjs
diff options
context:
space:
mode:
authorLibravatar Peter Simon <psi@estos.de>2024-02-28 06:41:45 +0100
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2024-02-28 13:40:05 +0000
commit912f563a9c547cddff672a464723119b572a4e7b (patch)
tree8c453550aae46bdba6263c45af2b6af7df39c85f /scripts/create.mjs
parentadd czukowski as a contributor for code [skip ci] (#512) (diff)
downloadferdium-recipes-912f563a9c547cddff672a464723119b572a4e7b.tar.gz
ferdium-recipes-912f563a9c547cddff672a464723119b572a4e7b.tar.zst
ferdium-recipes-912f563a9c547cddff672a464723119b572a4e7b.zip
porting to use ESM
Diffstat (limited to 'scripts/create.mjs')
-rw-r--r--scripts/create.mjs93
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 */
6import fs from 'fs-extra';
7
8import path from 'path';
9import open from 'open';
10
11if (process.argv.length < 3) {
12 console.log(`Usage: pnpm create <Recipe name> [Folder name]
13For example:
14pnpm create WhatsApp
15pnpm create "Google Hangouts"
16You can set "Folder name" to "FerdiumDev" to use Ferdium's development instance instead:
17
18pnpm create WhatsApp FerdiumDev
19`);
20 throw new Error('Please provide the correct number of args!');
21}
22
23const recipeName = process.argv[2];
24const recipe = recipeName.toLowerCase().replaceAll(/\s/g, '-');
25const folderName = process.argv[3] || 'Ferdium';
26const filesThatNeedTextReplace = ['package.json', 'index.js', 'webview.js'];
27
28const 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};
41const 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
89What'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})();