Skip to content

Commit 97a5b9a

Browse files
imhoffdikeith
andauthored
chore(cli): update plugin generation and plugin template (#3241)
Co-authored-by: Ian Keith <iankeith@gmail.com>
1 parent 5e84ce9 commit 97a5b9a

File tree

13 files changed

+103
-78
lines changed

13 files changed

+103
-78
lines changed

cli/src/tasks/new-plugin.ts

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ export async function newPlugin(config: Config) {
4646
{
4747
type: 'input',
4848
name: 'name',
49-
message: 'Plugin NPM name (kebab-case):',
49+
message: 'Plugin npm name (kebab-case. ex: capacitor-plugin-example):',
5050
validate: requiredInput
5151
},
5252
{
5353
type: 'input',
5454
name: 'domain',
55-
message: 'Plugin id (domain-style syntax. ex: com.example.plugin)',
55+
message: 'Plugin id (domain-style syntax. ex: com.mycompany.plugins.example)',
5656
validate: requiredInput
5757
},
5858
{
5959
type: 'input',
6060
name: 'className',
61-
message: 'Plugin class name (ex: AwesomePlugin)',
61+
message: 'Plugin class name (ex: Example)',
6262
validate: requiredInput
6363
},
6464
{
@@ -167,24 +167,24 @@ async function createIosPlugin(config: Config, pluginPath: string, domain: strin
167167
}
168168

169169
function generatePodspec(config: Config, answers: NewPluginAnswers) {
170-
return `
171-
require 'json'
172-
173-
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
174-
175-
Pod::Spec.new do |s|
176-
s.name = '${fixName(answers.name)}'
177-
s.version = package['version']
178-
s.summary = package['description']
179-
s.license = package['license']
180-
s.homepage = package['repository']['url']
181-
s.author = package['author']
182-
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
183-
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
184-
s.ios.deployment_target = '${config.ios.minVersion}'
185-
s.dependency 'Capacitor'
186-
s.swift_version = '5.0'
187-
end`;
170+
return `require 'json'
171+
172+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
173+
174+
Pod::Spec.new do |s|
175+
s.name = '${fixName(answers.name)}'
176+
s.version = package['version']
177+
s.summary = package['description']
178+
s.license = package['license']
179+
s.homepage = package['repository']['url']
180+
s.author = package['author']
181+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
182+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
183+
s.ios.deployment_target = '${config.ios.minVersion}'
184+
s.dependency 'Capacitor'
185+
s.swift_version = '5.1'
186+
end
187+
`;
188188
}
189189

190190
async function createAndroidPlugin(config: Config, pluginPath: string, domain: string, className: string) {
@@ -221,47 +221,61 @@ function generateAndroidManifest(domain: string, pluginPath: string) {
221221

222222
function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) {
223223
return {
224-
name: answers.name,
225-
version: '0.0.1',
226-
description: answers.description,
227-
main: 'dist/esm/index.js',
228-
types: 'dist/esm/index.d.ts',
229-
scripts: {
230-
'build': 'npm run clean && tsc',
224+
'name': answers.name,
225+
'version': '0.0.1',
226+
'description': answers.description,
227+
'main': 'dist/plugin.js',
228+
'module': 'dist/esm/index.js',
229+
'types': 'dist/esm/index.d.ts',
230+
'scripts': {
231+
'lint': 'npm run prettier -- --check && npm run swiftlint -- lint',
232+
'prettier': 'prettier "**/*.{css,html,ts,js,java}"',
233+
'swiftlint': 'node-swiftlint',
234+
'build': 'npm run clean && tsc && rollup -c rollup.config.js',
231235
'clean': 'rimraf ./dist',
232236
'watch': 'tsc --watch',
233237
'prepublishOnly': 'npm run build'
234238
},
235-
author: answers.author,
236-
license: answers.license,
237-
dependencies: {
238-
'@capacitor/core': `^${cliVersion}`
239-
},
240-
devDependencies: {
241-
'rimraf': '^3.0.0',
242-
'typescript': '^3.2.4',
239+
'author': answers.author,
240+
'license': answers.license,
241+
'devDependencies': {
242+
'@capacitor/android': `^${cliVersion}`,
243+
'@capacitor/core': `^${cliVersion}`,
243244
'@capacitor/ios': `^${cliVersion}`,
244-
'@capacitor/android': `^${cliVersion}`
245+
'@ionic/prettier-config': '^1.0.0',
246+
'@ionic/swiftlint-config': '^1.0.0',
247+
'@rollup/plugin-node-resolve': '^8.1.0',
248+
'prettier': '^2.0.5',
249+
'prettier-plugin-java': '^0.8.0',
250+
'rimraf': '^3.0.0',
251+
'rollup': '^2.21.0',
252+
'swiftlint': '^1.0.1',
253+
'typescript': '~3.8.3'
254+
},
255+
'peerDependencies': {
256+
'@capacitor/core': `^${cliVersion}`
245257
},
246-
files: [
258+
'files': [
247259
'dist/',
248260
'ios/',
249261
'android/',
250262
`${fixName(answers.name)}.podspec`
251263
],
252-
keywords: [
264+
'keywords': [
253265
'capacitor',
254266
'plugin',
255267
'native'
256268
],
257-
capacitor: {
258-
ios: {
259-
src: 'ios',
269+
'capacitor': {
270+
'ios': {
271+
'src': 'ios',
260272
},
261-
android: {
262-
src: 'android'
273+
'android': {
274+
'src': 'android'
263275
}
264276
},
277+
'prettier': '@ionic/prettier-config',
278+
'swiftlint': '@ionic/swiftlint-config',
265279
'repository': {
266280
'type': 'git',
267281
'url': answers.git

plugin-template/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
dist

plugin-template/android/Plugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import com.getcapacitor.PluginCall;
77
import com.getcapacitor.PluginMethod;
88

9-
@NativePlugin()
9+
@NativePlugin
1010
public class CLASS_NAME extends Plugin {
1111

12-
@PluginMethod()
12+
@PluginMethod
1313
public void echo(PluginCall call) {
1414
String value = call.getString("value");
1515

plugin-template/android/src/androidTest/java/com/getcapacitor/android/ExampleInstrumentedTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package com.getcapacitor.android;
22

3-
import android.content.Context;
3+
import static org.junit.Assert.*;
44

5-
import androidx.test.platform.app.InstrumentationRegistry;
5+
import android.content.Context;
66
import androidx.test.ext.junit.runners.AndroidJUnit4;
7-
7+
import androidx.test.platform.app.InstrumentationRegistry;
88
import org.junit.Test;
99
import org.junit.runner.RunWith;
1010

11-
import static org.junit.Assert.*;
12-
1311
/**
1412
* Instrumented test, which will execute on an Android device.
1513
*
1614
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
1715
*/
1816
@RunWith(AndroidJUnit4.class)
1917
public class ExampleInstrumentedTest {
18+
2019
@Test
2120
public void useAppContext() throws Exception {
2221
// Context of the app under test.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.getcapacitor;
22

3-
import org.junit.Test;
4-
53
import static org.junit.Assert.*;
64

5+
import org.junit.Test;
6+
77
/**
88
* Example local unit test, which will execute on the development machine (host).
99
*
1010
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
1111
*/
1212
public class ExampleUnitTest {
13+
1314
@Test
1415
public void addition_isCorrect() throws Exception {
1516
assertEquals(4, 2 + 2);
1617
}
17-
}
18+
}

plugin-template/ios/Plugin/Plugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Capacitor
77
*/
88
@objc(CLASS_NAME)
99
public class CLASS_NAME: CAPPlugin {
10-
10+
1111
@objc func echo(_ call: CAPPluginCall) {
1212
let value = call.getString("value") ?? ""
1313
call.success([

plugin-template/ios/PluginTests/PluginTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@ import Capacitor
33
@testable import Plugin
44

55
class PluginTests: XCTestCase {
6-
6+
77
override func setUp() {
88
super.setUp()
99
// Put setup code here. This method is called before the invocation of each test method in the class.
1010
}
11-
11+
1212
override func tearDown() {
1313
// Put teardown code here. This method is called after the invocation of each test method in the class.
1414
super.tearDown()
1515
}
16-
16+
1717
func testEcho() {
1818
// This is an example of a functional test case for a plugin.
1919
// Use XCTAssert and related functions to verify your tests produce the correct results.
20-
20+
2121
let value = "Hello, World!"
2222
let plugin = MyPlugin()
23-
23+
2424
let call = CAPPluginCall(callbackId: "test", options: [
2525
"value": value
26-
], success: { (result, call) in
26+
], success: { (result, _) in
2727
let resultValue = result!.data["value"] as? String
2828
XCTAssertEqual(value, resultValue)
29-
}, error: { (err) in
29+
}, error: { (_) in
3030
XCTFail("Error shouldn't have been called")
3131
})
32-
32+
3333
plugin.echo(call!)
3434
}
3535
}

plugin-template/ios/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ end
1313

1414
target 'PluginTests' do
1515
capacitor_pods
16-
end
16+
end

plugin-template/rollup.config.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
import nodeResolve from 'rollup-plugin-node-resolve';
1+
import nodeResolve from '@rollup/plugin-node-resolve';
22

33
export default {
44
input: 'dist/esm/index.js',
55
output: {
66
file: 'dist/plugin.js',
77
format: 'iife',
8-
name: 'capacitorPlugin',
9-
sourcemap: true
8+
name: 'capacitorPlugin', // TODO: change this
9+
globals: {
10+
'@capacitor/core': 'capacitorExports',
11+
},
12+
sourcemap: true,
1013
},
1114
plugins: [
12-
nodeResolve()
13-
]
14-
};
15+
nodeResolve({
16+
// allowlist of dependencies to bundle in
17+
// @see https://github.com/rollup/plugins/tree/master/packages/node-resolve#resolveonly
18+
resolveOnly: ['lodash'],
19+
}),
20+
],
21+
};

plugin-template/src/definitions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
declare module "@capacitor/core" {
1+
declare module '@capacitor/core' {
22
interface PluginRegistry {
33
Echo: EchoPlugin;
44
}
55
}
66

77
export interface EchoPlugin {
8-
echo(options: { value: string }): Promise<{value: string}>;
8+
echo(options: { value: string }): Promise<{ value: string }>;
99
}

0 commit comments

Comments
 (0)