|
1 | 1 | #!/usr/bin/env node
|
| 2 | +const fs = require('fs'); |
| 3 | +const prompts = require('prompts'); |
| 4 | +const { execSync } = require('child_process'); |
| 5 | +const { green, blue } = require('kleur'); |
2 | 6 |
|
3 |
| -const fs = require('fs') |
4 |
| -fs.copyFile('src/Config/index.example.js', 'src/Config/index.js', (err) => { |
5 |
| - if (err) throw err; |
6 |
| - console.log('src/Config/index.example.js was copied to src/Config/index.js'); |
| 7 | +prompts({ |
| 8 | + type: 'confirm', |
| 9 | + name: 'usingTs', |
| 10 | + message: 'Using typescript ?', |
| 11 | + initial: false |
| 12 | +}).then(async (response) => { |
| 13 | + if (response.usingTs) { |
| 14 | + await chooseTypescript(); |
| 15 | + fs.copyFile('src/Config/index.example.js', 'src/Config/index.ts', (err) => { |
| 16 | + if (err) throw err; |
| 17 | + fs.rename('src/Config/index.example.js', 'src/Config/index.example.ts', function(err) { |
| 18 | + if (err) throw err; |
| 19 | + }); |
| 20 | + }); |
| 21 | + execSync('yarn add -D typescript @types/jest @types/react @types/react-native @types/react-test-renderer @types/fbemitter @types/react-redux\n ', {stdio : 'pipe' }); |
| 22 | + } else { |
| 23 | + fs.copyFile('src/Config/index.example.js', 'src/Config/index.js', (err) => { |
| 24 | + if (err) throw err; |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + //Remove the ts-support |
| 29 | + fs.rmdir('ts-support-template', { recursive: true }, (err) => { |
| 30 | + if (err) throw err; |
| 31 | + }); |
| 32 | + |
| 33 | + printSuccess(response.usingTs); |
7 | 34 | });
|
| 35 | +// To improve: needed because the "Executing post init script" is on top of the prompt |
| 36 | +console.log('\n'); |
| 37 | + |
| 38 | +// Function that apply the ts support |
| 39 | +function chooseTypescript() { |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + // add the tsconfig.json |
| 42 | + fs.copyFile('ts-support-template/tsconfig.json', 'tsconfig.json', (err) => { |
| 43 | + if (err) throw err; |
| 44 | + }); |
| 45 | + overrideWithTypeScript(); |
| 46 | + renameWithTypeScript(); |
| 47 | + resolve(); |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +function overrideWithTypeScript(accumulatedPath = '/', tsRoot = 'ts-support-template') { |
| 52 | + return fs.readdirSync(`${tsRoot}${accumulatedPath}`).forEach((item) => { |
| 53 | + if (!fs.lstatSync(`${tsRoot}${accumulatedPath}${item}`).isDirectory()) { |
| 54 | + const [name] = item.split('.'); |
| 55 | + fs.copyFile(`${tsRoot}${accumulatedPath}${item}`, `.${accumulatedPath}${item}`, (err) => { |
| 56 | + if (err) throw err; |
| 57 | + }); |
| 58 | + if (fs.existsSync(`.${accumulatedPath}${name}.js`)) { |
| 59 | + fs.unlink(`.${accumulatedPath}${name}.js`, (err) => { |
| 60 | + if (err) throw err; |
| 61 | + }); |
| 62 | + } |
| 63 | + } else { |
| 64 | + overrideWithTypeScript(`${accumulatedPath}${item}/`); |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function renameWithTypeScript(accumulatedPath = '/', isTsx = true , jsRoot = 'src') { |
| 70 | + const EXCLUDED_DIRECTORIES = ['Assets', 'Config']; |
| 71 | + const TSX_DIRECTORIES = ['Components', 'Containers', 'Navigators']; |
| 72 | + return fs.readdirSync(`${jsRoot}${accumulatedPath}`).forEach((item) => { |
| 73 | + const [name, extension] = item.split('.'); |
| 74 | + if (!fs.lstatSync(`${jsRoot}${accumulatedPath}${item}`).isDirectory()) { |
| 75 | + if (extension === 'js') { |
| 76 | + let fileExtension = '.ts'; |
| 77 | + const firstCharIsUpperCase = name.charAt(0).toUpperCase() === name.charAt(0); |
| 78 | + if (isTsx && firstCharIsUpperCase) { |
| 79 | + fileExtension = '.tsx'; |
| 80 | + } |
| 81 | + fs.rename(`${jsRoot}${accumulatedPath}${item}`, `${jsRoot}${accumulatedPath}${name}${fileExtension}`, function(err) { |
| 82 | + if (err) throw err; |
| 83 | + }); |
| 84 | + } |
| 85 | + } else { |
| 86 | + const tsxCondition = accumulatedPath === '/' ? TSX_DIRECTORIES.includes(item) : isTsx || TSX_DIRECTORIES.includes(item); |
| 87 | + if (!EXCLUDED_DIRECTORIES.includes(item)) { |
| 88 | + renameWithTypeScript(`${accumulatedPath}${item}/`, tsxCondition); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +function printSuccess(isTs) { |
| 95 | + console.log("\n"); |
| 96 | + console.log("TheCodingMachine React-Native Boilerplate initialized with success !"); |
| 97 | + console.log("" + |
| 98 | + green(" .-` `:: \n" + |
| 99 | + " `///////////// `/shhhy+- ://. /sy/ /ss/ :NMN: \n" + |
| 100 | + " `sssssyhhhyhhy:` `yMMMMNNMMMd. osss: `hMMMh- .-` `+ss: +MMm/. \n" + |
| 101 | + " :MMM+----. `mMMm+:-`.oo/` osssyo` `dMMMMh- ohhh- `oyy+- sMMd/. \n" + |
| 102 | + " :MMM+` oMMN/- osssyhs`.mMMMMMh- -hds` .syy+- `hMMh:` \n" + |
| 103 | + " :MMM+` hMMd: oss-ohhhMMMhdMMh- `. +NNm/. .dMMy:` \n" + |
| 104 | + " :MMM+` oMMN/ oss-`shdMMd:dMMh- :-` sMMd/. -NMMo- \n" + |
| 105 | + " :MMM+` `mMMm/` `/+:. oss- `sdMd:.hMMh- hMys- `hMMh:` -hdy-` \n" + |
| 106 | + " :MMM+` `yMMMMdssss+ oss. `oh/- hMMh- -hyo: dMMs:` -ss+` \n" + |
| 107 | + " .oys:` .+yhyo/- -::` `` -syo- ..` +mo- `// \n" + |
| 108 | + " `` ``` `` `. ")); |
| 109 | + if (isTs) { |
| 110 | + console.log(blue("THE TYPESCRIPT VERSION")); |
| 111 | + } |
| 112 | + console.log("\n"); |
8 | 113 |
|
9 |
| -console.log("TheCodingMachine React-Native Boilerplate initialized with success !"); |
10 |
| -console.log("" + |
11 |
| - " .-` `:: \n" + |
12 |
| - " `///////////// `/shhhy+- ://. /sy/ /ss/ :NMN: \n" + |
13 |
| - " `sssssyhhhyhhy:` `yMMMMNNMMMd. osss: `hMMMh- .-` `+ss: +MMm/. \n" + |
14 |
| - " :MMM+----. `mMMm+:-`.oo/` osssyo` `dMMMMh- ohhh- `oyy+- sMMd/. \n" + |
15 |
| - " :MMM+` oMMN/- osssyhs`.mMMMMMh- -hds` .syy+- `hMMh:` \n" + |
16 |
| - " :MMM+` hMMd: oss-ohhhMMMhdMMh- `. +NNm/. .dMMy:` \n" + |
17 |
| - " :MMM+` oMMN/ oss-`shdMMd:dMMh- :-` sMMd/. -NMMo- \n" + |
18 |
| - " :MMM+` `mMMm/` `/+:. oss- `sdMd:.hMMh- hMys- `hMMh:` -hdy-` \n" + |
19 |
| - " :MMM+` `yMMMMdssss+ oss. `oh/- hMMh- -hyo: dMMs:` -ss+` \n" + |
20 |
| - " .oys:` .+yhyo/- -::` `` -syo- ..` +mo- `// \n" + |
21 |
| - " `` ``` `` `. ") |
22 |
| - |
23 |
| -console.log('- If you need to read more about this boilerplate : https://thecodingmachine.github.io/react-native-boilerplate/') |
24 |
| -console.log('- If you have some troubles : https://github.com/thecodingmachine/react-native-boilerplate/issues') |
25 |
| -console.log('- If you love this boilerplate, give us a star, you will be a ray of sunshine in our lives :) https://github.com/thecodingmachine/react-native-boilerplate') |
| 114 | + console.log('- If you need to read more about this boilerplate : https://thecodingmachine.github.io/react-native-boilerplate/'); |
| 115 | + console.log('- If you have some troubles : https://github.com/thecodingmachine/react-native-boilerplate/issues'); |
| 116 | + console.log('- If you love this boilerplate, give us a star, you will be a ray of sunshine in our lives :) https://github.com/thecodingmachine/react-native-boilerplate'); |
| 117 | +} |
0 commit comments