aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/create.js
diff options
context:
space:
mode:
authorLibravatar vantezzen <hello@vantezzen.io>2020-05-10 10:41:55 +0200
committerLibravatar vantezzen <hello@vantezzen.io>2020-05-10 10:41:55 +0200
commitd69e514dceb46a02cca4b73ea4fea487b93d71ad (patch)
tree34bf182e0a28d784f3e7d321d41ef330b347d0e4 /scripts/create.js
parentUpdate agent agent configuration (diff)
downloadferdium-recipes-d69e514dceb46a02cca4b73ea4fea487b93d71ad.tar.gz
ferdium-recipes-d69e514dceb46a02cca4b73ea4fea487b93d71ad.tar.zst
ferdium-recipes-d69e514dceb46a02cca4b73ea4fea487b93d71ad.zip
Add create script
Diffstat (limited to 'scripts/create.js')
-rw-r--r--scripts/create.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/scripts/create.js b/scripts/create.js
new file mode 100644
index 0000000..724eeb6
--- /dev/null
+++ b/scripts/create.js
@@ -0,0 +1,70 @@
1/**
2 * Create a new recipe for your service
3 */
4const fs = require('fs-extra');
5const path = require('path');
6const open = require('open');
7
8if (process.argv.length < 3) {
9 console.log(`Usage: npm run create <Recipe name> [Folder name]
10For example:
11npm run create WhatsApp
12npm run create "Google Hangouts"
13You can set "Folder name" to "FerdiDev" to use Ferdi's development instance instead:
14
15npm run create WhatsApp FerdiDev
16`);
17 return;
18}
19
20const recipeName = process.argv[2];
21const recipe = recipeName.toLowerCase().replace(/\s/g, '-');
22const cleanRecipeId = recipe.replace(/[^a-z]/g, ''); // Clean recipe ID only containing a-z, for usage as the JavaScript class name
23const folderName = process.argv[3] || 'Ferdi';
24const filesThatNeedTextReplace = ['package.json', 'index.js', 'README.md'];
25
26(async () => {
27 // Folder paths
28 const userData = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.config");
29 const recipesFolder = path.join(userData, folderName, "recipes");
30 const devRecipeFolder = path.join(recipesFolder, "dev");
31 const newRecipeFolder = path.join(devRecipeFolder, recipe);
32 const sampleRecipe = path.join(__dirname, 'sample_recipe');
33
34 // Make sure dev recipe folder exists
35 if (!await fs.exists(recipesFolder)) {
36 console.log(`Couldn't find your recipe folder (${recipesFolder}). Is Ferdi installed?`);
37 return;
38 }
39 await fs.ensureDir(devRecipeFolder);
40
41 if (await fs.exists(newRecipeFolder)) {
42 console.log('⚠️ Recipe already exists');
43 return;
44 }
45
46 console.log('[Info] Passed pre-checks');
47
48 // Copy sample recipe to recipe folder
49 await fs.copy(sampleRecipe, newRecipeFolder);
50 console.log('[Info] Copied recipe');
51
52 // Replace "SERVICE" with the service name
53 for (const file of filesThatNeedTextReplace) {
54 const filePath = path.join(newRecipeFolder, file);
55 let contents = await fs.readFile(filePath, 'utf-8');
56 contents = contents.replace(/SERVICE/g, recipe);
57 contents = contents.replace(/SNAME/g, recipeName);
58 contents = contents.replace(/SCLEAN/g, cleanRecipeId);
59 await fs.writeFile(filePath, contents);
60 }
61 console.log('[Info] Prepared new recipe');
62
63 open(newRecipeFolder);
64 console.log(`✅ Successfully created your recipe.
65
66What's next?
67- Make sure you restart Ferdi in order for the recipe to show up
68- Customise "webview.js", "package.json", "icon.svg" and "icon.png (see https://github.com/getferdi/recipes/blob/master/docs/integration.md#recipe-structure)
69- Publish and package your recipe (see https://github.com/getferdi/recipes/blob/master/docs/integration.md#publishing)`);
70})();