Skip to content

Commit

Permalink
removed template and updated cli
Browse files Browse the repository at this point in the history
  • Loading branch information
orYoffe committed Nov 2, 2020
1 parent 583a99e commit 08c97b5
Show file tree
Hide file tree
Showing 88 changed files with 50 additions and 5,557 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### A simple CLI tool to start your React Native Web project to develop same app for IOS Android and Web

**Compatibility: React Native >= 0.63**.

<p align="center" >
<img width="300" src="https://raw.githubusercontent.com/orYoffe/create-react-native-web-app/master/template/src/logo.png">
</p>
Expand Down
129 changes: 19 additions & 110 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const chalk = require('chalk');
const commander = require('commander');
const fs = require('fs-extra');
const path = require('path');
// const fs = require('fs-extra');
// const path = require('path');
const execSync = require('child_process').execSync;
const packageJson = require('./package.json');
const copyFiles = require('./copyFiles');
// const copyFiles = require('./copyFiles');

const nodeVersion = process.versions.node;
const nodeVersionSplitted = nodeVersion.split('.');
Expand All @@ -24,23 +24,19 @@ if (nodeMajorVersion < 8) {
}

const printCyan = (text) => console.log(` ${chalk.cyan(text)}`);
const printGreen = (text) => console.log(` ${chalk.green(text)}`);
// const printGreen = (text) => console.log(` ${chalk.green(text)}`);

let appName;
let appBundleId;
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-directory> [<bundle-id>]')
.arguments('<project-directory>')
.option('-r, --router')
.usage(
`${chalk.green('<project-directory>')} [${chalk.green('<bundle-id>')}]`,
)
.action((name, bundleId) => {
.usage(`${chalk.green('<project-directory>')}`)
.action((name) => {
appName = name;
appBundleId = bundleId;
})
.on('--help', () => {
console.log(` Only ${chalk.green('<project-directory>')} is required.`);
console.log(` ${chalk.green('<project-directory>')} is required.`);
console.log();
console.log(
` If you have any problems, do not hesitate to file an issue:`,
Expand All @@ -54,114 +50,27 @@ const program = new commander.Command(packageJson.name)

async function run() {
if (appName) {
function installPods() {
// TODO add better testing for pods
if (process.platform === 'darwin') {
const iosFolderPath = `${appName}/ios`;
try {
if (!fs.existsSync(iosFolderPath)) {
return;
}

const hasPods = fs.existsSync(`${iosFolderPath}/Podfile`);

if (!hasPods) {
return;
}

try {
// Check if "pod" is available and usable. It happens that there are
// multiple versions of "pod" command and even though it's there, it exits
// with a failure
execSync(`cd ${iosFolderPath} && pod --version`);
} catch (e) {
// "pod" command outputs errors to stdout (at least some of them)
console.log(error.stderr || error.stdout);

throw new Error(
`Failed to install CocoaPods dependencies for iOS project, which is required by this template.\nPlease try again manually: "gem install cocoapods --no-document && cd ./${appName}/ios && pod install".\nCocoaPods documentation:
"https://cocoapods.org/"
`,
);
}

try {
printCyan(
'⏳ Installing CocoaPods dependencies (this may take a few minutes)...',
);
execSync(`cd ${iosFolderPath} && pod install`);
} catch (error) {
// "pod" command outputs errors to stdout (at least some of them)
console.log(error.stderr || error.stdout);

throw new Error(
`Failed to install CocoaPods dependencies for iOS project, which is required by this template.\nPlease try again manually: "cd ./${appName}/ios && pod install".\nCocoaPods documentation:
"https://cocoapods.org/"
`,
);
}
} catch (error) {
throw error;
}
}
}

printCyan(`⏳ Creating React Native Web App by the name of ${appName}`);
console.log();

printCyan('⏳ Creating project folder...');
console.log();

// create folder appName and copy files
fs.ensureDirSync(appName);
fs.emptyDirSync(appName);

printCyan('⏳ Adding project files...');
console.log();

await copyFiles(path.resolve(__dirname, 'template'), appName);
execSync(
`npx react-native init ${appName} --template react-native-template-react-native-web`,
{stdio: [0, 1, 2]},
);

if (program.router) {
printCyan('⏳ Adding react router files...');
console.log();
fs.removeSync(`${appName}/src/App.js`);
await copyFiles(
path.resolve(__dirname, 'react-router'),
path.resolve(appName, 'src'),
);
let isYarnAvailable;
try {
execSync('yarnpkg --version', {stdio: 'ignore'});
isYarnAvailable = true;
} catch (e) {
isYarnAvailable = false;
}

fs.renameSync(
path.resolve(appName, '.gitignore.txt'),
path.resolve(appName, '.gitignore'),
);

try {
execSync(`cd ${appName} && git init`);
} catch (error) {}

// install deps
printCyan('⏳ Installing project dependencies...');
console.log();

const renameCommand = `cd ${appName} && npx react-native-rename-next ${appName}${
appBundleId ? ` -b ${appBundleId}` : ''
}`;

execSync(renameCommand);

const installCommand = `cd ${appName} && npm i${
program.router ? '&& npm i -S react-router-native react-router-dom' : ''
}`;
execSync(installCommand, {stdio: [0, 1, 2]});

installPods();

// print script commands with info links
printGreen('✅ Done! 😁👍 Your project is ready for development.');
console.log();

const packageManagerRunCommand = 'npm run';
const packageManagerRunCommand = isYarnAvailable ? 'yarn' : 'npm run';
console.log(`
${chalk.magenta('*')} ${chalk.magenta(
'change directory to your new project',
Expand Down
90 changes: 24 additions & 66 deletions cli.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
const chalk = require('chalk');
// const commander = require("commander");
const fs = require('fs-extra');
// const path = require("path");
const execSync = require('child_process').execSync;
const copyFiles = require('./copyFiles');
// const packageJson = require("./package.json");
const isWin = process.platform === 'win32';

jest.mock('fs-extra', () => {
const ensureDirSync = jest.fn();
const emptyDirSync = jest.fn();
const copySync = jest.fn();
const existsSync = jest.fn();
const renameSync = jest.fn();

return {
ensureDirSync,
emptyDirSync,
copySync,
existsSync,
renameSync,
};
});
jest.mock('./copyFiles', () => {
const copyFiles = jest.fn(() => Promise.resolve());

return copyFiles;
});

const originalConsoleLog = console.log;
console.log = jest.fn();
console.log = jest.fn(() => true);

const originalConsoleError = console.error;
console.error = jest.fn();
Expand Down Expand Up @@ -57,32 +30,30 @@ jest.mock('chalk', () => {
describe('cli runs properly', () => {
it('cli runs with argument and logs info', (done) => {
process.argv[2] = 'myFakeName';
fs.existsSync.mockReturnValue(true);
jest.requireActual('./cli');

let isYarnAvailable;
try {
execSync('yarnpkg --version', {stdio: 'ignore'});
isYarnAvailable = true;
} catch (e) {
isYarnAvailable = false;
}

const packageManagerRunCommand = isYarnAvailable ? 'yarn' : 'npm run';
process.nextTick(() => {
expect(chalk.red.mock.calls).toEqual([]);
expect(chalk.cyan.mock.calls).toEqual([
['⏳ Creating React Native Web App by the name of myFakeName'],
['⏳ Creating project folder...'],
['⏳ Adding project files...'],
['⏳ Installing project dependencies...'],
[
'⏳ Installing CocoaPods dependencies (this may take a few minutes)...',
],
['cd myFakeName'],
['Then run the these commands to get started:'],
['npm run web'],
['npm run android'],
['npm run ios'],
['npm run test'],
['npm run build'],
]);
expect(chalk.green.mock.calls).toEqual([
['<project-directory>'],
['<bundle-id>'],
['✅ Done! 😁👍 Your project is ready for development.'],
[`${packageManagerRunCommand} web`],
[`${packageManagerRunCommand} android`],
[`${packageManagerRunCommand} ios`],
[`${packageManagerRunCommand} test`],
[`${packageManagerRunCommand} build`],
]);
expect(chalk.green.mock.calls).toEqual([['<project-directory>']]);
expect(chalk.magenta.mock.calls).toEqual([
['*'],
['change directory to your new project'],
Expand All @@ -102,27 +73,14 @@ describe('cli runs properly', () => {
['To run build for Web'],
]);

if (isWin) {
expect(execSync.mock.calls).toEqual([
[
'cd myFakeName && npx react-native-rename-next myFakeName && npm i',
{stdio: [0, 1, 2]},
],
]);
} else {
expect(execSync.mock.calls).toEqual([
['cd myFakeName && git init'],
['cd myFakeName && npx react-native-rename-next myFakeName'],
['cd myFakeName && npm i', {stdio: [0, 1, 2]}],
['cd myFakeName/ios && pod --version'],
['cd myFakeName/ios && pod install'],
]);
}

expect(fs.emptyDirSync.mock.calls).toEqual([['myFakeName']]);
expect(fs.ensureDirSync.mock.calls).toEqual([['myFakeName']]);
expect(copyFiles.mock.calls).toEqual([
[__dirname + (isWin ? '\\' : '/') + 'template', 'myFakeName'],
expect(execSync.mock.calls).toEqual([
[
'npx react-native init myFakeName --template react-native-template-react-native-web',
{stdio: [0, 1, 2]},
],
['yarnpkg --version', {stdio: 'ignore'}],
['cd myFakeName && git init'],
['yarnpkg --version', {stdio: 'ignore'}],
]);
});
done();
Expand Down
84 changes: 0 additions & 84 deletions copyFiles.js

This file was deleted.

Loading

0 comments on commit 08c97b5

Please sign in to comment.