From b7e62220c8b2245d6fe1865834344529502612a5 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Thu, 9 Jul 2020 11:55:02 -0700 Subject: [PATCH 01/12] more clear examples --- cli/src/tasks/new-plugin.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index 6f9a22e4c..d8f9ac646 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -46,19 +46,19 @@ export async function newPlugin(config: Config) { { type: 'input', name: 'name', - message: 'Plugin NPM name (kebab-case):', + message: 'Plugin npm name (kebab-case. ex: capacitor-plugin-example):', validate: requiredInput }, { type: 'input', name: 'domain', - message: 'Plugin id (domain-style syntax. ex: com.example.plugin)', + message: 'Plugin id (domain-style syntax. ex: com.example.plugins.example)', validate: requiredInput }, { type: 'input', name: 'className', - message: 'Plugin class name (ex: AwesomePlugin)', + message: 'Plugin class name (ex: Example)', validate: requiredInput }, { From a9a63e0063a311023c66ebdc8b11ec27e2b8767b Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Thu, 9 Jul 2020 11:55:16 -0700 Subject: [PATCH 02/12] fix whitespace --- cli/src/tasks/new-plugin.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index d8f9ac646..d92657952 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -167,24 +167,24 @@ async function createIosPlugin(config: Config, pluginPath: string, domain: strin } function generatePodspec(config: Config, answers: NewPluginAnswers) { - return ` - require 'json' - - package = JSON.parse(File.read(File.join(__dir__, 'package.json'))) - - Pod::Spec.new do |s| - s.name = '${fixName(answers.name)}' - s.version = package['version'] - s.summary = package['description'] - s.license = package['license'] - s.homepage = package['repository']['url'] - s.author = package['author'] - s.source = { :git => package['repository']['url'], :tag => s.version.to_s } - s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}' - s.ios.deployment_target = '${config.ios.minVersion}' - s.dependency 'Capacitor' - s.swift_version = '5.0' - end`; + return `require 'json' + +package = JSON.parse(File.read(File.join(__dir__, 'package.json'))) + +Pod::Spec.new do |s| + s.name = '${fixName(answers.name)}' + s.version = package['version'] + s.summary = package['description'] + s.license = package['license'] + s.homepage = package['repository']['url'] + s.author = package['author'] + s.source = { :git => package['repository']['url'], :tag => s.version.to_s } + s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}' + s.ios.deployment_target = '${config.ios.minVersion}' + s.dependency 'Capacitor' + s.swift_version = '5.0' +end +`; } async function createAndroidPlugin(config: Config, pluginPath: string, domain: string, className: string) { From e097bf73037b89d722c1ae4937fb3c238e2ff716 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Fri, 10 Jul 2020 15:04:19 -0700 Subject: [PATCH 03/12] lock in typescript version --- cli/src/tasks/new-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index d92657952..ab17b9b6e 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -239,7 +239,7 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { }, devDependencies: { 'rimraf': '^3.0.0', - 'typescript': '^3.2.4', + 'typescript': '~3.8.3', '@capacitor/ios': `^${cliVersion}`, '@capacitor/android': `^${cliVersion}` }, From 3feb678acbc94e3aa18b155161052af66774a781 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Fri, 10 Jul 2020 15:08:34 -0700 Subject: [PATCH 04/12] tsconfig cleanup --- plugin-template/tsconfig.json | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugin-template/tsconfig.json b/plugin-template/tsconfig.json index 4377384b4..9f458f6e8 100644 --- a/plugin-template/tsconfig.json +++ b/plugin-template/tsconfig.json @@ -1,19 +1,21 @@ { "compilerOptions": { - "allowSyntheticDefaultImports": true, + "allowUnreachableCode": false, "declaration": true, - "experimentalDecorators": true, + "esModuleInterop": true, "lib": [ "dom", "es2015" ], "module": "es2015", "moduleResolution": "node", - "noImplicitAny": true, + "noFallthroughCasesInSwitch": true, "noUnusedLocals": true, "noUnusedParameters": true, "outDir": "dist/esm", + "pretty": true, "sourceMap": true, + "strict": true, "target": "es2015" }, "files": [ From 4c5804595aca13600c5a90e7f0086a0d684c55ae Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Fri, 10 Jul 2020 15:37:03 -0700 Subject: [PATCH 05/12] rollup --- cli/src/tasks/new-plugin.ts | 46 +++++++++++++++++--------------- plugin-template/rollup.config.js | 19 ++++++++----- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index ab17b9b6e..2620420fa 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -221,44 +221,48 @@ function generateAndroidManifest(domain: string, pluginPath: string) { function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { return { - name: answers.name, - version: '0.0.1', - description: answers.description, - main: 'dist/esm/index.js', - types: 'dist/esm/index.d.ts', - scripts: { - 'build': 'npm run clean && tsc', + 'name': answers.name, + 'version': '0.0.1', + 'description': answers.description, + 'main': 'dist/plugin.js', + 'module': 'dist/esm/index.js', + 'types': 'dist/esm/index.d.ts', + 'scripts': { + 'build': 'npm run clean && tsc && rollup -c rollup.config.js', 'clean': 'rimraf ./dist', 'watch': 'tsc --watch', 'prepublishOnly': 'npm run build' }, - author: answers.author, - license: answers.license, - dependencies: { - '@capacitor/core': `^${cliVersion}` - }, - devDependencies: { - 'rimraf': '^3.0.0', - 'typescript': '~3.8.3', + 'author': answers.author, + 'license': answers.license, + 'devDependencies': { + '@capacitor/android': `^${cliVersion}`, + '@capacitor/core': `^${cliVersion}`, '@capacitor/ios': `^${cliVersion}`, - '@capacitor/android': `^${cliVersion}` + '@rollup/plugin-node-resolve': '^8.1.0', + 'rimraf': '^3.0.0', + 'rollup': '^2.21.0', + 'typescript': '~3.8.3' + }, + 'peerDependencies': { + '@capacitor/core': `^${cliVersion}` }, - files: [ + 'files': [ 'dist/', 'ios/', 'android/', `${fixName(answers.name)}.podspec` ], - keywords: [ + 'keywords': [ 'capacitor', 'plugin', 'native' ], - capacitor: { - ios: { + 'capacitor': { + 'ios': { src: 'ios', }, - android: { + 'android': { src: 'android' } }, diff --git a/plugin-template/rollup.config.js b/plugin-template/rollup.config.js index 906e8a15e..6994412fb 100644 --- a/plugin-template/rollup.config.js +++ b/plugin-template/rollup.config.js @@ -1,14 +1,21 @@ -import nodeResolve from 'rollup-plugin-node-resolve'; +import nodeResolve from '@rollup/plugin-node-resolve'; export default { input: 'dist/esm/index.js', output: { file: 'dist/plugin.js', format: 'iife', - name: 'capacitorPlugin', - sourcemap: true + name: 'capacitorPlugin', // TODO: change this + globals: { + '@capacitor/core': 'capacitorExports', + }, + sourcemap: true, }, plugins: [ - nodeResolve() - ] -}; \ No newline at end of file + nodeResolve({ + // allowlist of dependencies to bundle in + // @see https://github.com/rollup/plugins/tree/master/packages/node-resolve#resolveonly + resolveOnly: ['lodash'], + }), + ], +}; From 77ec74db4c112400be1e7cd075d926143da7c999 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Fri, 10 Jul 2020 15:51:06 -0700 Subject: [PATCH 06/12] newlines --- plugin-template/ios/Podfile | 2 +- plugin-template/src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin-template/ios/Podfile b/plugin-template/ios/Podfile index 8f4ebba29..350751435 100644 --- a/plugin-template/ios/Podfile +++ b/plugin-template/ios/Podfile @@ -13,4 +13,4 @@ end target 'PluginTests' do capacitor_pods -end \ No newline at end of file +end diff --git a/plugin-template/src/index.ts b/plugin-template/src/index.ts index 6181377f0..5c5939590 100644 --- a/plugin-template/src/index.ts +++ b/plugin-template/src/index.ts @@ -1,2 +1,2 @@ export * from './definitions'; -export * from './web'; \ No newline at end of file +export * from './web'; From 6f7f9c39f13762c1ccf9395794b2f48bff638601 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Mon, 13 Jul 2020 14:06:26 -0700 Subject: [PATCH 07/12] implement prettier --- cli/src/tasks/new-plugin.ts | 5 +++++ plugin-template/.prettierignore | 2 ++ plugin-template/android/Plugin.java | 4 ++-- .../getcapacitor/android/ExampleInstrumentedTest.java | 9 ++++----- .../src/test/java/com/getcapacitor/ExampleUnitTest.java | 7 ++++--- plugin-template/src/definitions.ts | 4 ++-- plugin-template/src/web.ts | 4 ++-- 7 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 plugin-template/.prettierignore diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index 2620420fa..b403d2a29 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -228,6 +228,7 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { 'module': 'dist/esm/index.js', 'types': 'dist/esm/index.d.ts', 'scripts': { + 'prettier': 'prettier "**/*.{css,html,ts,js,java}"', 'build': 'npm run clean && tsc && rollup -c rollup.config.js', 'clean': 'rimraf ./dist', 'watch': 'tsc --watch', @@ -239,7 +240,10 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { '@capacitor/android': `^${cliVersion}`, '@capacitor/core': `^${cliVersion}`, '@capacitor/ios': `^${cliVersion}`, + '@ionic/prettier-config': '^1.0.0', '@rollup/plugin-node-resolve': '^8.1.0', + 'prettier': '^2.0.5', + 'prettier-plugin-java': '^0.8.0', 'rimraf': '^3.0.0', 'rollup': '^2.21.0', 'typescript': '~3.8.3' @@ -266,6 +270,7 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { src: 'android' } }, + 'prettier': '@ionic/prettier-config', 'repository': { 'type': 'git', 'url': answers.git diff --git a/plugin-template/.prettierignore b/plugin-template/.prettierignore new file mode 100644 index 000000000..9d0b71a3c --- /dev/null +++ b/plugin-template/.prettierignore @@ -0,0 +1,2 @@ +build +dist diff --git a/plugin-template/android/Plugin.java b/plugin-template/android/Plugin.java index e8c98124c..e0f47c482 100644 --- a/plugin-template/android/Plugin.java +++ b/plugin-template/android/Plugin.java @@ -6,10 +6,10 @@ import com.getcapacitor.PluginCall; import com.getcapacitor.PluginMethod; -@NativePlugin() +@NativePlugin public class CLASS_NAME extends Plugin { - @PluginMethod() + @PluginMethod public void echo(PluginCall call) { String value = call.getString("value"); diff --git a/plugin-template/android/src/androidTest/java/com/getcapacitor/android/ExampleInstrumentedTest.java b/plugin-template/android/src/androidTest/java/com/getcapacitor/android/ExampleInstrumentedTest.java index 64f83739d..58020e16c 100644 --- a/plugin-template/android/src/androidTest/java/com/getcapacitor/android/ExampleInstrumentedTest.java +++ b/plugin-template/android/src/androidTest/java/com/getcapacitor/android/ExampleInstrumentedTest.java @@ -1,15 +1,13 @@ package com.getcapacitor.android; -import android.content.Context; +import static org.junit.Assert.*; -import androidx.test.platform.app.InstrumentationRegistry; +import android.content.Context; import androidx.test.ext.junit.runners.AndroidJUnit4; - +import androidx.test.platform.app.InstrumentationRegistry; import org.junit.Test; import org.junit.runner.RunWith; -import static org.junit.Assert.*; - /** * Instrumented test, which will execute on an Android device. * @@ -17,6 +15,7 @@ */ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { + @Test public void useAppContext() throws Exception { // Context of the app under test. diff --git a/plugin-template/android/src/test/java/com/getcapacitor/ExampleUnitTest.java b/plugin-template/android/src/test/java/com/getcapacitor/ExampleUnitTest.java index 06806a775..a0fed0cfb 100644 --- a/plugin-template/android/src/test/java/com/getcapacitor/ExampleUnitTest.java +++ b/plugin-template/android/src/test/java/com/getcapacitor/ExampleUnitTest.java @@ -1,17 +1,18 @@ package com.getcapacitor; -import org.junit.Test; - import static org.junit.Assert.*; +import org.junit.Test; + /** * Example local unit test, which will execute on the development machine (host). * * @see Testing documentation */ public class ExampleUnitTest { + @Test public void addition_isCorrect() throws Exception { assertEquals(4, 2 + 2); } -} \ No newline at end of file +} diff --git a/plugin-template/src/definitions.ts b/plugin-template/src/definitions.ts index 21baf1ba5..23d624e94 100644 --- a/plugin-template/src/definitions.ts +++ b/plugin-template/src/definitions.ts @@ -1,9 +1,9 @@ -declare module "@capacitor/core" { +declare module '@capacitor/core' { interface PluginRegistry { Echo: EchoPlugin; } } export interface EchoPlugin { - echo(options: { value: string }): Promise<{value: string}>; + echo(options: { value: string }): Promise<{ value: string }>; } diff --git a/plugin-template/src/web.ts b/plugin-template/src/web.ts index 9e73d7059..d3bccb308 100644 --- a/plugin-template/src/web.ts +++ b/plugin-template/src/web.ts @@ -5,11 +5,11 @@ export class MyPluginWeb extends WebPlugin implements MyPluginPlugin { constructor() { super({ name: 'MyPlugin', - platforms: ['web'] + platforms: ['web'], }); } - async echo(options: { value: string }): Promise<{value: string}> { + async echo(options: { value: string }): Promise<{ value: string }> { console.log('ECHO', options); return options; } From 240df53ebef94b6057141d0cc647540862c5f9c0 Mon Sep 17 00:00:00 2001 From: Dan Imhoff Date: Mon, 13 Jul 2020 14:20:35 -0700 Subject: [PATCH 08/12] Update cli/src/tasks/new-plugin.ts Co-authored-by: Ian Keith --- cli/src/tasks/new-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index b403d2a29..8b3254d33 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -182,7 +182,7 @@ Pod::Spec.new do |s| s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}' s.ios.deployment_target = '${config.ios.minVersion}' s.dependency 'Capacitor' - s.swift_version = '5.0' + s.swift_version = '5.1' end `; } From be5ab92ac891d0ec26c6370bcdbdc708745f790d Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Mon, 13 Jul 2020 16:00:32 -0700 Subject: [PATCH 09/12] implement swiftlint --- cli/src/tasks/new-plugin.ts | 5 +++++ plugin-template/ios/Plugin/Plugin.swift | 2 +- .../ios/PluginTests/PluginTests.swift | 16 ++++++++-------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index 8b3254d33..7330412fa 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -228,7 +228,9 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { 'module': 'dist/esm/index.js', 'types': 'dist/esm/index.d.ts', 'scripts': { + 'lint': 'npm run prettier -- --check && npm run swiftlint -- lint', 'prettier': 'prettier "**/*.{css,html,ts,js,java}"', + 'swiftlint': 'swiftlint', 'build': 'npm run clean && tsc && rollup -c rollup.config.js', 'clean': 'rimraf ./dist', 'watch': 'tsc --watch', @@ -241,11 +243,13 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { '@capacitor/core': `^${cliVersion}`, '@capacitor/ios': `^${cliVersion}`, '@ionic/prettier-config': '^1.0.0', + '@ionic/swiftlint-config': '^1.0.0', '@rollup/plugin-node-resolve': '^8.1.0', 'prettier': '^2.0.5', 'prettier-plugin-java': '^0.8.0', 'rimraf': '^3.0.0', 'rollup': '^2.21.0', + 'swiftlint': '^0.39.4', 'typescript': '~3.8.3' }, 'peerDependencies': { @@ -271,6 +275,7 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { } }, 'prettier': '@ionic/prettier-config', + 'swiftlint': '@ionic/swiftlint-config', 'repository': { 'type': 'git', 'url': answers.git diff --git a/plugin-template/ios/Plugin/Plugin.swift b/plugin-template/ios/Plugin/Plugin.swift index 287b53601..f76f3994c 100644 --- a/plugin-template/ios/Plugin/Plugin.swift +++ b/plugin-template/ios/Plugin/Plugin.swift @@ -7,7 +7,7 @@ import Capacitor */ @objc(CLASS_NAME) public class CLASS_NAME: CAPPlugin { - + @objc func echo(_ call: CAPPluginCall) { let value = call.getString("value") ?? "" call.success([ diff --git a/plugin-template/ios/PluginTests/PluginTests.swift b/plugin-template/ios/PluginTests/PluginTests.swift index 00f036e4c..4e0259932 100644 --- a/plugin-template/ios/PluginTests/PluginTests.swift +++ b/plugin-template/ios/PluginTests/PluginTests.swift @@ -3,33 +3,33 @@ import Capacitor @testable import Plugin class PluginTests: XCTestCase { - + override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. } - + override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() } - + func testEcho() { // This is an example of a functional test case for a plugin. // Use XCTAssert and related functions to verify your tests produce the correct results. - + let value = "Hello, World!" let plugin = MyPlugin() - + let call = CAPPluginCall(callbackId: "test", options: [ "value": value - ], success: { (result, call) in + ], success: { (result, _) in let resultValue = result!.data["value"] as? String XCTAssertEqual(value, resultValue) - }, error: { (err) in + }, error: { (_) in XCTFail("Error shouldn't have been called") }) - + plugin.echo(call!) } } From f38bed2e2d28300586be9da25f181e8d7e8f918c Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Wed, 15 Jul 2020 09:01:18 -0700 Subject: [PATCH 10/12] wrap src with single quotes --- cli/src/tasks/new-plugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index 7330412fa..5a90b43a8 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -268,10 +268,10 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { ], 'capacitor': { 'ios': { - src: 'ios', + 'src': 'ios', }, 'android': { - src: 'android' + 'src': 'android' } }, 'prettier': '@ionic/prettier-config', From 5db61c7f4a9cc526904a457d9a2759f1fd812ae4 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Wed, 15 Jul 2020 09:01:42 -0700 Subject: [PATCH 11/12] use mycompany for domain name --- cli/src/tasks/new-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index 5a90b43a8..81cbf1843 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -52,7 +52,7 @@ export async function newPlugin(config: Config) { { type: 'input', name: 'domain', - message: 'Plugin id (domain-style syntax. ex: com.example.plugins.example)', + message: 'Plugin id (domain-style syntax. ex: com.mycompany.plugins.example)', validate: requiredInput }, { From 77c9391b4b13c3cee8bcbe86bb660105d8910d60 Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Thu, 16 Jul 2020 11:12:44 -0700 Subject: [PATCH 12/12] update swiftlint --- cli/src/tasks/new-plugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/tasks/new-plugin.ts b/cli/src/tasks/new-plugin.ts index 81cbf1843..33ee31716 100644 --- a/cli/src/tasks/new-plugin.ts +++ b/cli/src/tasks/new-plugin.ts @@ -230,7 +230,7 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { 'scripts': { 'lint': 'npm run prettier -- --check && npm run swiftlint -- lint', 'prettier': 'prettier "**/*.{css,html,ts,js,java}"', - 'swiftlint': 'swiftlint', + 'swiftlint': 'node-swiftlint', 'build': 'npm run clean && tsc && rollup -c rollup.config.js', 'clean': 'rimraf ./dist', 'watch': 'tsc --watch', @@ -249,7 +249,7 @@ function generatePackageJSON(answers: NewPluginAnswers, cliVersion: string) { 'prettier-plugin-java': '^0.8.0', 'rimraf': '^3.0.0', 'rollup': '^2.21.0', - 'swiftlint': '^0.39.4', + 'swiftlint': '^1.0.1', 'typescript': '~3.8.3' }, 'peerDependencies': {