Skip to content

Commit b3a3029

Browse files
committed
refactor(source): add padua, update dependencies and switch to ES Module
1 parent 5c40dba commit b3a3029

File tree

12 files changed

+155
-143
lines changed

12 files changed

+155
-143
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
node_modules
2-
package-lock.json
1+
node_modules
2+
package-lock.json
3+
jsconfig.json

customize.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { join } = require('path')
2-
const { readFileSync, writeFileSync } = require('fs-extra')
1+
import { join } from 'path'
2+
import { readFileSync, writeFileSync } from 'fs'
33

44
const files = [
55
'app/App.js',
@@ -9,19 +9,19 @@ const files = [
99
'.flowconfig',
1010
'create-app.js',
1111
'package.json',
12-
'README.md'
12+
'README.md',
1313
]
1414

1515
// Replace template values with plugin name.
16-
module.exports = (name, directory) => {
17-
const replaceTemplateVariables = file => {
16+
export default (name, directory) => {
17+
const replaceTemplateVariables = (file) => {
1818
const filePath = join(directory, file)
1919
let contents = readFileSync(filePath, 'utf-8')
2020

2121
contents = contents.replace(/<%= name %>/g, name.regular)
2222
contents = contents.replace(/<%= pascal %>/g, name.pascal)
2323

24-
writeFileSync(filePath, contents, 'utf-8')
24+
writeFileSync(filePath, contents)
2525
}
2626

2727
files.forEach(replaceTemplateVariables)

index.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env node
2-
3-
const { resolve } = require('path')
4-
const { ensureDirSync, emptyDirSync, existsSync, copySync } = require('fs-extra')
5-
const { execSync } = require('child_process')
6-
const names = require('./names')
7-
const customize = require('./customize')
2+
import { join } from 'path'
3+
import { existsSync, unlinkSync, rmdirSync, lstatSync, mkdirSync } from 'fs'
4+
import { copySync } from 'fs-extra'
5+
import { execSync } from 'child_process'
6+
import names from './names.js'
7+
import customize from './customize.js'
88

99
const args = process.argv
10+
1011
if (args.length < 3) {
1112
return console.error('Please provide a name for your plugin.')
1213
}
@@ -19,24 +20,34 @@ if (existsSync(name.regular)) {
1920
)
2021
}
2122

22-
ensureDirSync(name.regular)
23-
emptyDirSync(name.regular)
23+
if (existsSync(name.regular)) {
24+
if (lstatSync(name.regular).isDirectory()) {
25+
rmdirSync(name.regular, { recursive: true })
26+
} else {
27+
unlinkSync(name.regular)
28+
}
29+
}
2430

25-
const templateDirectory = resolve(__dirname, 'template')
31+
mkdirSync(name.regular)
2632

27-
const destinationDirectory = resolve(process.cwd(), name.regular)
33+
const templateDirectory = join(__dirname, 'template')
34+
const destinationDirectory = join(process.cwd(), name.regular)
2835

2936
copySync(templateDirectory, destinationDirectory)
3037

3138
customize(name, destinationDirectory)
3239

3340
console.log('Installing dependencies...')
3441

35-
execSync('npm i', { cwd: name.regular, stdio : 'pipe' })
42+
execSync('npm i', { cwd: destinationDirectory, stdio: 'pipe' })
3643

3744
console.log('')
38-
console.log(`😃 Created new plugin called ${name.regular} in ${destinationDirectory}.`)
45+
console.log(
46+
`😃 Created new plugin called ${name.regular} in ${destinationDirectory}.`
47+
)
3948
console.log(`🛠️ Start coding in the file src/index.js.`)
40-
console.log(`🛠️ To preview the plugin edit app/App.js and create a RN installation with:`)
49+
console.log(
50+
`🛠️ To preview the plugin edit app/App.js and create a RN installation with:`
51+
)
4152
console.log(`🐚 cd ${name.regular}`)
4253
console.log('🐚 npm run app')

names.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const { pascalCase } = require('change-case')
1+
import { pascalCase } from 'change-case'
22

3-
module.exports = name => {
3+
export default (name) => {
44
// react-native prefix only used to identify package out of RN context.
55
const unprefixed = name.replace(/^react-native-/, '')
66

77
return {
88
regular: name,
9-
pascal: pascalCase(unprefixed)
9+
pascal: pascalCase(unprefixed),
1010
}
1111
}

package.json

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
{
22
"name": "create-react-native-plugin",
33
"description": "Template for creating React Native plugins without native code.",
4-
"author": "Matthias Giger",
5-
"license": "MIT",
64
"version": "1.0.1",
7-
"repository": "github:naminho/create-react-native-plugin",
8-
"main": "index.js",
9-
"bin": {
10-
"create-react-native-plugin": "./index.js",
11-
"crnp": "./index.js"
5+
"repository": "github:tobua/create-react-native-plugin",
6+
"license": "MIT",
7+
"author": "Matthias Giger",
8+
"padua": {
9+
"source": true
1210
},
1311
"dependencies": {
14-
"change-case": "4.1.0",
15-
"fs-extra": "8.1.0"
12+
"change-case": "4.1.2",
13+
"fs-extra": "9.1.0"
14+
},
15+
"type": "module",
16+
"main": "index.js",
17+
"sideEffects": false,
18+
"bin": "index.js",
19+
"source": "index.js",
20+
"files": [
21+
"**/*.js"
22+
],
23+
"devDependencies": {
24+
"padua": "^0.3.5"
25+
},
26+
"prettier": "padua/configuration/.prettierrc.json",
27+
"eslintConfig": {
28+
"extends": "./node_modules/padua/configuration/eslint.cjs",
29+
"rules": {
30+
"import/extensions": [
31+
2,
32+
"ignorePackages"
33+
]
34+
}
1635
},
17-
"prettier": {
18-
"semi": false,
19-
"singleQuote": true
36+
"engines": {
37+
"node": ">= 14"
2038
}
2139
}

template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ To test your plugin on a device run the following to create a React Native app u
3737

3838
```
3939
npm install
40-
npm run app --silent
40+
npm run app
4141
cd app
4242
react-native run-ios / react-native run-android
4343
```

template/app/App.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
2-
import React from 'react';
3-
import {StyleSheet, View, Text} from 'react-native';
4-
import <%= pascal %> from '<%= name %>';
2+
import React from 'react'
3+
import { StyleSheet, View, Text } from 'react-native'
4+
import <%= pascal %> from '<%= name %>'
55

66
export default () => (
77
<View style={styles.screen}>
@@ -10,7 +10,7 @@ export default () => (
1010
<<%= pascal %> initialCount={45} />
1111
<Text style={styles.subtitle}>create-react-native-plugin</Text>
1212
</View>
13-
);
13+
)
1414

1515
const styles = StyleSheet.create({
1616
screen: {
@@ -26,4 +26,4 @@ const styles = StyleSheet.create({
2626
subtitle: {
2727
fontSize: 15,
2828
},
29-
});
29+
})

template/create-app.js

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,40 @@
11
#!/usr/bin/env node
2-
3-
import {join} from 'path';
4-
import {promisify} from 'util';
5-
import {exec} from 'child_process';
6-
import copy from 'recursive-copy';
7-
import rimraf from 'rimraf';
2+
import { join } from 'path'
3+
import { execSync } from 'child_process'
4+
import copy from 'recursive-copy'
5+
import rimraf from 'rimraf'
86

97
// Enhances source files inside /app with a fresh RN project template.
10-
(async () => {
11-
const appName = 'MyPluginApp';
12-
const execute = promisify(exec);
13-
14-
console.log('Initializing a fresh RN project...');
15-
console.log('⌛ This might take some time.');
16-
17-
// Remove local CLI, as it interferes with the global.
18-
rimraf.sync('node_modules/@react-native-community/cli');
19-
// Remove existing installation in case it got stuck last time.
20-
rimraf.sync(`app/${appName}`);
21-
22-
const timer = setTimeout(() => {
23-
// Warning after 5 minutes in case native dependencies missing for CLI.
24-
console.log(
25-
"If it's stuck at this point, try creating a RN project elsewhere with 'npx react-native init TestApp' and follow the instructions there before trying this again.",
26-
);
27-
}, 300000);
28-
29-
// Initialize RN project.
30-
await execute(`npx react-native init ${appName}`, {
31-
cwd: 'app',
32-
});
33-
34-
clearTimeout(timer);
35-
36-
// Copy to destination directory, leaving source files untouched.
37-
await copy(`app/${appName}`, 'app', {
38-
dot: true,
39-
overwrite: false,
40-
filter: ['**/*', '!App.js'],
41-
});
42-
43-
// Remove temporary project directory.
44-
rimraf.sync(`app/${appName}`);
45-
46-
// Install this package locally, avoiding symlinks.
47-
await execute('npm install $(npm pack .. | tail -1)', {
48-
cwd: join(__dirname, 'app'),
49-
});
50-
51-
console.log('');
52-
console.log('🍞 React Native App created inside /app.');
53-
console.log('🛠️ To run the example with the plugin included:');
54-
console.log('🐚 cd app');
55-
console.log('🐚 react-native run-ios / react-native run-android');
56-
console.log('🌪️ To copy over the changes from the plugin source run:');
57-
console.log('🐚 npm run watch');
58-
console.log('🛠️ This will copy changes over to the app.');
59-
})();
8+
const appName = 'MyPluginApp'
9+
10+
console.log('⌛ Initializing a fresh RN project...')
11+
12+
execSync(`npx react-native init ${appName}`, {
13+
// Write output to cnosole.
14+
stdio: 'inherit',
15+
})
16+
17+
// Copy to destination directory, leaving source files untouched.
18+
await copy(appName, 'app', {
19+
dot: true,
20+
overwrite: false,
21+
filter: ['**/*', '!App.js'],
22+
})
23+
24+
// Remove temporary project directory.
25+
rimraf.sync(appName)
26+
27+
// Install this package locally, avoiding symlinks.
28+
execSync('npm install $(npm pack .. | tail -1)', {
29+
cwd: join(process.cwd(), 'app'),
30+
stdio: 'inherit',
31+
})
32+
33+
console.log('')
34+
console.log('🍞 React Native App created inside /app.')
35+
console.log('🛠️ To run the example with the plugin included:')
36+
console.log('🐚 cd app')
37+
console.log('🐚 react-native run-ios / react-native run-android')
38+
console.log('🌪️ To copy over the changes from the plugin source run:')
39+
console.log('🐚 npm run watch')
40+
console.log('🛠️ This will copy changes over to the app.')

template/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
"license": "MIT",
55
"main": "src/index.js",
66
"scripts": {
7-
"app": "babel-node create-app",
7+
"app": "node create-app.js",
88
"watch": "cpx 'src/**/*' app/node_modules/<%= name %>/src --watch",
99
"test": "jest --watchAll",
1010
"lint": "eslint ./src --fix",
1111
"format": "prettier \"src/**/*\" --write"
1212
},
1313
"prettier": {
14-
"bracketSpacing": false,
15-
"jsxBracketSameLine": true,
1614
"singleQuote": true,
17-
"trailingComma": "all"
15+
"semi": false
1816
},
1917
"eslintConfig": {
2018
"extends": "@react-native-community"
@@ -47,5 +45,8 @@
4745
"react-test-renderer": "^16.12.0",
4846
"recursive-copy": "^2.0.10",
4947
"rimraf": "^3.0.0"
48+
},
49+
"engines": {
50+
"node": ">= 14"
5051
}
5152
}

0 commit comments

Comments
 (0)