forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-firebase-module.js
124 lines (116 loc) · 4.36 KB
/
create-firebase-module.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const { join, resolve } = require('path');
const { readdirSync, renameSync, statSync } = require('fs');
const shelljs = require('shelljs');
const inquirer = require('inquirer');
const { version } = require('./../lerna');
function walkDir(dir) {
let results = [];
const list = readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
const stat = statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(walkDir(file));
} else {
results.push(file);
}
});
return results;
}
inquirer
.prompt([
{
type: 'input',
name: 'name',
message: 'What is the name of this module, e.g. analytics:',
default: 'iid',
},
])
.then(answers => {
const { name } = answers;
const packageDir = `packages/${name}`;
const nameUpper = name.charAt(0).toUpperCase() + name.slice(1);
shelljs.cp('-R', 'scripts/_TEMPLATE_', packageDir);
shelljs.exec(
`find ./${packageDir}/ -type f -exec sed -i '' -e "s/_template_/${name}/g" {} \\;`,
);
shelljs.exec(
`find ./${packageDir}/ -type f -exec sed -i '' -e "s/_Template_/${nameUpper}/g" {} \\;`,
);
shelljs.exec(
`find ./${packageDir}/package.json -type f -exec sed -i '' -e "s/_VERSION_/${version}/g" {} \\;`,
);
return Promise.resolve({ name, nameUpper });
})
.then(({ name, nameUpper }) => {
const dir = resolve(`./packages/${name}/`);
const templateFiles = walkDir(dir).filter(file => {
return file.includes('_template_') || file.includes('_Template_');
});
shelljs.mv(`${dir}/ios/RNFB_Template_`, `${dir}/ios/RNFB${nameUpper}`);
shelljs.mv(`${dir}/ios/RNFB_Template_.xcodeproj`, `${dir}/ios/RNFB${nameUpper}.xcodeproj`);
shelljs.mv(
`${dir}/android/src/main/java/io/invertase/firebase/_template_`,
`${dir}/android/src/main/java/io/invertase/firebase/${name}`,
);
for (let i = 0; i < templateFiles.length; i++) {
const templateFile = templateFiles[i]
.replace(
`${dir}/android/src/main/java/io/invertase/firebase/_template_`,
`${dir}/android/src/main/java/io/invertase/firebase/${name}`,
)
.replace(`${dir}/ios/RNFB_Template_/`, `${dir}/ios/RNFB${nameUpper}/`)
.replace(`${dir}/ios/RNFB_Template_.x`, `${dir}/ios/RNFB${nameUpper}.x`);
const newTemplateFilePath = templateFile
.replace(/_template_/g, name)
.replace(/_Template_/g, nameUpper);
renameSync(templateFile, newTemplateFilePath);
}
return Promise.resolve({ name, nameUpper });
})
.then(({ name, nameUpper }) => {
shelljs.exec(
`lerna add @react-native-firebase/${name} tests && yarn`,
);
console.log('');
console.log(`The module '${name}' (${nameUpper}) has been created!`);
console.log('');
console.log('');
console.log('TO USE IT ADD TO THE TESTING PROJECT:');
console.log('');
console.log(' iOS:');
console.log(' Add the following to tests/ios/Podfile :');
console.log(
` pod 'RNFB${nameUpper}', :path => '../../packages/${name}/ios/RNFB${nameUpper}.podspec', :version => "~> #{rnfb_version}"`,
);
console.log('');
console.log('');
console.log(' Android:');
console.log(' Add the following to tests/android/settings.gradle :');
console.log(` include ':@react-native-firebase/${name}'`);
console.log(
` project(':@react-native-firebase/${name}').projectDir = new File(rootProject.projectDir, './../../packages/${name}/android')`,
);
console.log('');
console.log(' Add the following dependency to tests/android/app/build.gradle :');
console.log(` implementation project(path: ':@react-native-firebase/${name}')`);
console.log('');
console.log('');
})
.catch(console.error);