@capacitor/create-plugin 0.20.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,4 +30,5 @@ As of the `0.8.0` release, example apps for testing are included when initializi
30
30
  --author <author> ......... Author name and email (e.g. "Name <name@example.com>")
31
31
  --license <id> ............ SPDX License ID (e.g. "MIT")
32
32
  --description <text> ...... Short description of plugin features
33
+ --android-lang <text> ..... Language for Android plugin development (either "kotlin" or "java")
33
34
  ```
Binary file
Binary file
package/dist/help.js CHANGED
@@ -13,6 +13,7 @@ const help = `
13
13
  --author <author> ......... Author name and email (e.g. "Name <name@example.com>")
14
14
  --license <id> ............ SPDX License ID (e.g. "MIT")
15
15
  --description <text> ...... Short description of plugin features
16
+ --android-lang ............ Language for Android plugin development (either "kotlin" or "java")
16
17
 
17
18
  -h, --help ................ Print help, then quit
18
19
  --verbose ................. Print verbose output to stderr
package/dist/index.js CHANGED
@@ -13,6 +13,15 @@ const prompt_1 = require("./prompt");
13
13
  const subprocess_1 = require("./subprocess");
14
14
  const template_1 = require("./template");
15
15
  const debug = (0, debug_1.default)('@capacitor/create-plugin');
16
+ const isInsideGitRepo = async (cwd) => {
17
+ try {
18
+ await (0, subprocess_1.runSilent)('git', ['rev-parse', '--is-inside-work-tree'], { cwd, stdio: 'ignore' });
19
+ return true;
20
+ }
21
+ catch (e) {
22
+ return false;
23
+ }
24
+ };
16
25
  process.on('unhandledRejection', (error) => {
17
26
  process.stderr.write(`ERR: ${error}\n`);
18
27
  process.exit(1);
@@ -99,15 +108,21 @@ const run = async () => {
99
108
  catch (e) {
100
109
  process.stderr.write(`WARN: Could not create test application: ${(_f = (_e = e.message) !== null && _e !== void 0 ? _e : e.stack) !== null && _f !== void 0 ? _f : e}\n`);
101
110
  }
102
- process.stdout.write('Initializing git...\n');
103
- try {
104
- await (0, subprocess_1.run)('git', ['init'], opts);
105
- await (0, subprocess_1.run)('git', ['checkout', '-b', 'main'], opts);
106
- await (0, subprocess_1.run)('git', ['add', '-A'], opts);
107
- await (0, subprocess_1.run)('git', ['commit', '-m', 'Initial commit', '--no-gpg-sign'], opts);
111
+ const isInGitRepo = await isInsideGitRepo(details.dir);
112
+ if (isInGitRepo) {
113
+ process.stdout.write('Skipping git initialization (already inside a git repository)...\n');
108
114
  }
109
- catch (e) {
110
- process.stderr.write(`WARN: Could not initialize git: ${(_h = (_g = e.message) !== null && _g !== void 0 ? _g : e.stack) !== null && _h !== void 0 ? _h : e}\n`);
115
+ else {
116
+ process.stdout.write('Initializing git...\n');
117
+ try {
118
+ await (0, subprocess_1.run)('git', ['init'], opts);
119
+ await (0, subprocess_1.run)('git', ['checkout', '-b', 'main'], opts);
120
+ await (0, subprocess_1.run)('git', ['add', '-A'], opts);
121
+ await (0, subprocess_1.run)('git', ['commit', '-m', 'Initial commit', '--no-gpg-sign'], opts);
122
+ }
123
+ catch (e) {
124
+ process.stderr.write(`WARN: Could not initialize git: ${(_h = (_g = e.message) !== null && _g !== void 0 ? _g : e.stack) !== null && _h !== void 0 ? _h : e}\n`);
125
+ }
111
126
  }
112
127
  const tada = (0, cli_1.emoji)('🎉', '*');
113
128
  process.stdout.write(`
package/dist/options.js CHANGED
@@ -6,7 +6,16 @@ const debug_1 = tslib_1.__importDefault(require("debug"));
6
6
  const cli_1 = require("./cli");
7
7
  const debug = (0, debug_1.default)('@capacitor/create-plugin:options');
8
8
  const CLI_ARGS = ['dir'];
9
- const CLI_OPTIONS = ['name', 'package-id', 'class-name', 'repo', 'author', 'license', 'description'];
9
+ const CLI_OPTIONS = [
10
+ 'name',
11
+ 'package-id',
12
+ 'class-name',
13
+ 'repo',
14
+ 'author',
15
+ 'license',
16
+ 'description',
17
+ 'android-lang',
18
+ ];
10
19
  exports.VALIDATORS = {
11
20
  name: (value) => typeof value !== 'string' || value.trim().length === 0
12
21
  ? `Must provide a plugin name, e.g. "capacitor-plugin-example"`
@@ -31,6 +40,9 @@ exports.VALIDATORS = {
31
40
  author: () => true,
32
41
  license: (value) => typeof value !== 'string' || value.trim().length === 0 ? `Must provide a valid license, e.g. "MIT"` : true,
33
42
  description: (value) => typeof value !== 'string' || value.trim().length === 0 ? `Must provide a description` : true,
43
+ 'android-lang': (value) => typeof value === 'string' && value.trim().length > 0 && /^(kotlin|kt|java)$/i.test(value)
44
+ ? true
45
+ : `Must be either "kotlin" or "java"`,
34
46
  dir: (value) => typeof value !== 'string' || value.trim().length === 0
35
47
  ? `Must provide a directory, e.g. "my-plugin"`
36
48
  : /^-/.test(value)
package/dist/prompt.js CHANGED
@@ -76,6 +76,15 @@ const gatherDetails = (initialOptions) => {
76
76
  message: `Enter a SPDX license identifier for your plugin.\n`,
77
77
  validate: options_1.VALIDATORS.license,
78
78
  },
79
+ {
80
+ type: 'select',
81
+ name: 'android-lang',
82
+ message: `What language would you like to use for your Android plugin?\n`,
83
+ choices: [
84
+ { title: 'Kotlin', value: 'kotlin' },
85
+ { title: 'Java', value: 'java' },
86
+ ],
87
+ },
79
88
  {
80
89
  type: 'text',
81
90
  name: 'description',
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wait = exports.run = exports.spawn = void 0;
3
+ exports.wait = exports.runSilent = exports.run = exports.spawn = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const cp = tslib_1.__importStar(require("child_process"));
6
6
  const kleur_1 = tslib_1.__importDefault(require("kleur"));
@@ -10,6 +10,10 @@ const run = async (cmd, args, options) => {
10
10
  await (0, exports.wait)((0, exports.spawn)(cmd, args, options));
11
11
  };
12
12
  exports.run = run;
13
+ const runSilent = async (cmd, args, options) => {
14
+ await (0, exports.wait)((0, exports.spawn)(cmd, args, options));
15
+ };
16
+ exports.runSilent = runSilent;
13
17
  const wait = async (p) => {
14
18
  return new Promise((resolve, reject) => {
15
19
  p.on('error', reject);
package/dist/template.js CHANGED
@@ -19,6 +19,7 @@ exports.readPackageJson = readPackageJson;
19
19
  const extractTemplate = async (dir, details, type) => {
20
20
  const templateFiles = [];
21
21
  const templateFolders = [];
22
+ const androidLang = details['android-lang'].toLowerCase();
22
23
  await (0, promises_1.mkdir)(dir, { recursive: true });
23
24
  await (0, tar_1.extract)({
24
25
  file: type === 'PLUGIN_TEMPLATE' ? TEMPLATE_PATH : WWW_TEMPLATE_PATH,
@@ -34,11 +35,39 @@ const extractTemplate = async (dir, details, type) => {
34
35
  },
35
36
  });
36
37
  await Promise.all(templateFiles.map((p) => (0, path_1.resolve)(dir, p)).map((p) => (0, exports.applyTemplate)(p, details)));
37
- await Promise.all(templateFolders.map((p) => (0, path_1.resolve)(dir, p)).map((p) => (0, promises_1.rmdir)(p)));
38
+ await deleteUnnecessaryFolders(dir, androidLang);
39
+ await Promise.all(templateFolders.map((p) => (0, path_1.resolve)(dir, p)).map((p) => (0, promises_1.rm)(p, { recursive: true })));
38
40
  };
39
41
  exports.extractTemplate = extractTemplate;
40
- const applyTemplate = async (p, { name, 'package-id': packageId, 'class-name': className, repo, author, license, description }) => {
42
+ const deleteUnnecessaryFolders = async (dir, androidLang) => {
43
+ const androidSrcDir = (0, path_1.join)(dir, 'android', 'src');
44
+ const sourceSets = ['main', 'test', 'androidTest'];
45
+ for (const sourceSet of sourceSets) {
46
+ const sourceFolder = (0, path_1.join)(androidSrcDir, sourceSet);
47
+ const javaFolder = (0, path_1.join)(sourceFolder, 'java');
48
+ const kotlinFolder = (0, path_1.join)(sourceFolder, 'kotlin');
49
+ if (androidLang === 'kotlin' && (await folderExists(javaFolder))) {
50
+ await (0, promises_1.rm)(javaFolder, { recursive: true });
51
+ }
52
+ if (androidLang === 'java' && (await folderExists(kotlinFolder))) {
53
+ await (0, promises_1.rm)(kotlinFolder, { recursive: true });
54
+ }
55
+ }
56
+ };
57
+ const folderExists = async (folderPath) => {
58
+ try {
59
+ const files = await (0, promises_1.readdir)(folderPath);
60
+ return files != null;
61
+ }
62
+ catch (err) {
63
+ return false;
64
+ }
65
+ };
66
+ const applyTemplate = async (p, { name, 'package-id': packageId, 'class-name': className, repo, author, license, description, 'android-lang': androidLang, }) => {
41
67
  const template = await (0, promises_1.readFile)(p, { encoding: 'utf8' });
68
+ const conditionalView = {
69
+ KOTLIN: androidLang.toLowerCase() !== 'java',
70
+ };
42
71
  const view = {
43
72
  CAPACITOR_VERSION: exports.CAPACITOR_VERSION,
44
73
  PACKAGE_NAME: name,
@@ -50,12 +79,16 @@ const applyTemplate = async (p, { name, 'package-id': packageId, 'class-name': c
50
79
  AUTHOR: author,
51
80
  LICENSE: license,
52
81
  DESCRIPTION: description,
82
+ ANDROID_LANG: androidLang,
53
83
  };
54
- const contents = mustache_1.default.render(template, view);
55
- const filePath = Object.entries(view).reduce((acc, [key, value]) => (value ? acc.replaceAll(`__${key}__`, value) : acc), p.substring(0, p.length - MUSTACHE_EXTENSION.length));
84
+ const combinedView = { ...view, ...conditionalView };
85
+ const intermediateContents = mustache_1.default.render(template, combinedView);
86
+ const finalContents = mustache_1.default.render(intermediateContents, view);
87
+ let filePath = p.substring(0, p.length - MUSTACHE_EXTENSION.length);
88
+ filePath = Object.entries(view).reduce((acc, [key, value]) => (value ? acc.replaceAll(`__${key}__`, value.toString()) : acc), filePath);
56
89
  await (0, promises_1.mkdir)((0, path_1.dirname)(filePath), { recursive: true });
57
90
  // take off the .mustache extension and write the file, then remove the template
58
- await (0, promises_1.writeFile)(filePath, contents, { encoding: 'utf8' });
91
+ await (0, promises_1.writeFile)(filePath, finalContents, { encoding: 'utf8' });
59
92
  await (0, promises_1.unlink)(p);
60
93
  };
61
94
  exports.applyTemplate = applyTemplate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/create-plugin",
3
- "version": "0.20.2",
3
+ "version": "0.21.0",
4
4
  "description": "Generate a new Capacitor plugin",
5
5
  "author": "Ionic Team <hi@ionicframework.com>",
6
6
  "homepage": "https://capacitorjs.com",