-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathruncap.js
63 lines (54 loc) · 1.83 KB
/
runcap.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* A simple facade to 'ionic cap' command that first copies the app specific
* capacitor config file into capacitor.config.ts and then spawns the
* 'ionic cap' with the arguments provided to this command.
*
* This command relies on the --project parameter to deduce the app's name,
* and therefore this parameter is required.
*
* The app specific capacitor config files are to be named as:
*
* capacitor-<project>.config.ts.
*
* Since capacitor.config.ts is transient, it's added to .gitignore.
*/
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { spawn } = require('child_process');
const fs = require('fs');
const capacitorConfig = `
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
android: {
path: './apps/<project>/android'
}
};
export default config;
`;
const args = yargs(hideBin(process.argv))
.option('project', {
type: 'string',
description: 'Capacitor app to execute commands on.'
})
.demandOption("project")
.argv
const CAPACITOR_CONFIG_FILE = 'capacitor.config.ts';
// Create the bare capacitor.config.ts for the given project
fs.writeFileSync(
CAPACITOR_CONFIG_FILE,
capacitorConfig.replace("<project>", args.project)
);
// Copy the project specific capacitor config file into
// capacitor.config.ts.
// require('fs').copyFile(`capacitor-${args.project}.config.ts`, CAPACITOR_CONFIG_FILE, (err) => {
// if (err) throw err;
// })
// Now run 'ionic cap <args[2:]>'
const capacitorArgs = process.argv.splice(2);
let ionicCommand = `ionic cap ${capacitorArgs.join(' ')}`;
// console.log(`ionic cmd: ${ionicCommand}`);
const child = spawn(ionicCommand, [], { shell: true, stdio: 'inherit' });
// This is optional. We can very well leave the file there.
child.on('close', (code) => {
fs.unlinkSync(CAPACITOR_CONFIG_FILE);
});