diff options
author | vantezzen <hello@vantezzen.io> | 2020-05-10 10:41:55 +0200 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2020-05-10 10:41:55 +0200 |
commit | d69e514dceb46a02cca4b73ea4fea487b93d71ad (patch) | |
tree | 34bf182e0a28d784f3e7d321d41ef330b347d0e4 /scripts/create.js | |
parent | Update agent agent configuration (diff) | |
download | ferdium-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.js | 70 |
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 | */ | ||
4 | const fs = require('fs-extra'); | ||
5 | const path = require('path'); | ||
6 | const open = require('open'); | ||
7 | |||
8 | if (process.argv.length < 3) { | ||
9 | console.log(`Usage: npm run create <Recipe name> [Folder name] | ||
10 | For example: | ||
11 | npm run create WhatsApp | ||
12 | npm run create "Google Hangouts" | ||
13 | You can set "Folder name" to "FerdiDev" to use Ferdi's development instance instead: | ||
14 | |||
15 | npm run create WhatsApp FerdiDev | ||
16 | `); | ||
17 | return; | ||
18 | } | ||
19 | |||
20 | const recipeName = process.argv[2]; | ||
21 | const recipe = recipeName.toLowerCase().replace(/\s/g, '-'); | ||
22 | const cleanRecipeId = recipe.replace(/[^a-z]/g, ''); // Clean recipe ID only containing a-z, for usage as the JavaScript class name | ||
23 | const folderName = process.argv[3] || 'Ferdi'; | ||
24 | const 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 | |||
66 | What'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 | })(); | ||