Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] refactor: use exported @react-native-community/cli init function to generate example - DRAFT WIP #420

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions lib/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');

const { info, warn, error } = require('console');

const { init } = require('@react-native-community/cli')

// default execa object
const execaDefault = require('execa');

Expand Down Expand Up @@ -130,20 +132,10 @@ exampleReactNativeVersion: ${exampleReactNativeVersion}
const commandSync = execa.commandSync;

if (generateExample) {
const reactNativeVersionCommand = 'react-native --version';
const yarnVersionCommand = 'yarn --version';

const checkCliOptions = { stdio: 'inherit' };
const errorRemedyMessage = 'both react-native-cli and yarn CLI tools are needed to generate example project';

try {
info('CREATE: Check for valid react-native-cli tool version, as needed to generate the example project');
commandSync(reactNativeVersionCommand, checkCliOptions);
info(`${reactNativeVersionCommand} ok`);
} catch (e) {
throw new Error(
`${reactNativeVersionCommand} failed; ${errorRemedyMessage}`);
}
const errorRemedyMessage = 'yarn CLI is needed to generate example project';

try {
info('CREATE: Check for valid Yarn CLI tool version, as needed to generate the example project');
Expand Down Expand Up @@ -195,22 +187,21 @@ exampleReactNativeVersion: ${exampleReactNativeVersion}
// multiple test or sample apps in the future.
const generateExampleApp =
() => {
const exampleReactNativeInitCommand =
`react-native init ${exampleName} --version ${exampleReactNativeVersion}`;
// resolve **absolute** module & example paths before
// react-native-community/cli init(Compat) function call
// which may affect the process cwd state
// (absolute paths are needed for the steps below to work)
const modulePath = path.resolve('.', moduleName);
const pathExampleApp = path.join(modulePath, exampleName);

info(
`CREATE example app with the following command: ${exampleReactNativeInitCommand}`);

const execOptions = { cwd: `./${moduleName}`, stdio: 'inherit' };
info(`INIT example app in ${moduleName}/${exampleName}`);

// (with the work done in a promise chain)
return Promise.resolve()
.then(() => {
// We use synchronous execSync / commandSync call here
// which is able to output its stdout to stdout in this process.
// Note that any exception would be properly handled since this
// call is executed within a Promise.resolve().then() callback.
commandSync(exampleReactNativeInitCommand, execOptions);
// @react-native-community/cli function call
// (...)
return init(`./${moduleName}/${exampleName}`, exampleName);
})
.then(() => {
// Render the example template
Expand All @@ -226,16 +217,13 @@ exampleReactNativeVersion: ${exampleReactNativeVersion}

return Promise.all(
exampleTemplates.map((template) => {
return renderTemplateIfValid(fs, moduleName, template, templateArgs);
return renderTemplateIfValid(fs, modulePath, template, templateArgs);
})
);
})
.then(() => {
// Adds and link the new library
return new Promise((resolve, reject) => {
// determine this path before the next steps:
const pathExampleApp = `./${moduleName}/${exampleName}`;

// postinstall workaround script *only* needed in case
// the DEPRECATED --example-file-linkage option
// (exampleFileLinkage: true) is used
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"homepage": "https://github.com/brodybits/create-react-native-module#readme",
"dependencies": {
"@react-native-community/cli": "^4.14.0",
"commander": "^6.2.1",
"console": "^0.7.2",
"execa": "^5.0.0",
Expand All @@ -54,6 +55,7 @@
"param-case": "^3.0.4",
"pascal-case": "^3.1.2",
"please-upgrade-node": "^3.2.0",
"react-native": "^0.63.4",
"update-notifier": "^5.1.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,11 @@ content:
",
"* execa.commandSync command: react-native init example --version react-native@latest options: {\\"cwd\\":\\"./react-native-alice-bobbi\\",\\"stdio\\":\\"inherit\\"}
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* outputFile name: react-native-alice-bobbi/example/metro.config.js
"* outputFile name: .../react-native-alice-bobbi/example/metro.config.js
content:
--------
// metro.config.js
Expand Down Expand Up @@ -832,7 +832,7 @@ module.exports = {

<<<<<<<< ======== >>>>>>>>
",
"* outputFile name: react-native-alice-bobbi/example/App.js
"* outputFile name: .../react-native-alice-bobbi/example/App.js
content:
--------
/**
Expand Down Expand Up @@ -883,11 +883,11 @@ const styles = StyleSheet.create({

<<<<<<<< ======== >>>>>>>>
",
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\"./react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\".../react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod install options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod install options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,11 @@ content:
",
"* execa.commandSync command: react-native init test-demo --version [email protected] options: {\\"cwd\\":\\"./react-native-alice-bobbi\\",\\"stdio\\":\\"inherit\\"}
",
"* ensureDir dir: react-native-alice-bobbi/test-demo/
"* ensureDir dir: .../react-native-alice-bobbi/test-demo/
",
"* ensureDir dir: react-native-alice-bobbi/test-demo/
"* ensureDir dir: .../react-native-alice-bobbi/test-demo/
",
"* outputFile name: react-native-alice-bobbi/test-demo/metro.config.js
"* outputFile name: .../react-native-alice-bobbi/test-demo/metro.config.js
content:
--------
// metro.config.js
Expand Down Expand Up @@ -832,7 +832,7 @@ module.exports = {

<<<<<<<< ======== >>>>>>>>
",
"* outputFile name: react-native-alice-bobbi/test-demo/App.js
"* outputFile name: .../react-native-alice-bobbi/test-demo/App.js
content:
--------
/**
Expand Down Expand Up @@ -883,11 +883,11 @@ const styles = StyleSheet.create({

<<<<<<<< ======== >>>>>>>>
",
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\"./react-native-alice-bobbi/test-demo\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\".../react-native-alice-bobbi/test-demo\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\"./react-native-alice-bobbi/test-demo/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\".../react-native-alice-bobbi/test-demo/ios\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod install options: {\\"cwd\\":\\"./react-native-alice-bobbi/test-demo/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod install options: {\\"cwd\\":\\".../react-native-alice-bobbi/test-demo/ios\\",\\"stdio\\":\\"inherit\\"}
",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ sdk.dir=/Users/{username}/Library/Android/sdk
",
"* execa.commandSync command: react-native init example --version react-native@latest options: {\\"cwd\\":\\"./react-native-alice-bobbi\\",\\"stdio\\":\\"inherit\\"}
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* outputFile name: react-native-alice-bobbi/example/metro.config.js
"* outputFile name: .../react-native-alice-bobbi/example/metro.config.js
content:
--------
// metro.config.js
Expand Down Expand Up @@ -425,7 +425,7 @@ module.exports = {

<<<<<<<< ======== >>>>>>>>
",
"* outputFile name: react-native-alice-bobbi/example/App.js
"* outputFile name: .../react-native-alice-bobbi/example/App.js
content:
--------
/**
Expand Down Expand Up @@ -488,7 +488,7 @@ const styles = StyleSheet.create({

<<<<<<<< ======== >>>>>>>>
",
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\"./react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\".../react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,11 @@ content:
",
"* execa.commandSync command: react-native init example --version react-native@latest options: {\\"cwd\\":\\"./react-native-alice-bobbi\\",\\"stdio\\":\\"inherit\\"}
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* outputFile name: react-native-alice-bobbi/example/metro.config.js
"* outputFile name: .../react-native-alice-bobbi/example/metro.config.js
content:
--------
// metro.config.js
Expand Down Expand Up @@ -828,7 +828,7 @@ module.exports = {

<<<<<<<< ======== >>>>>>>>
",
"* outputFile name: react-native-alice-bobbi/example/App.js
"* outputFile name: .../react-native-alice-bobbi/example/App.js
content:
--------
/**
Expand Down Expand Up @@ -891,11 +891,11 @@ const styles = StyleSheet.create({

<<<<<<<< ======== >>>>>>>>
",
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\"./react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\".../react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod install options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod install options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,11 @@ content:
",
"* execa.commandSync command: react-native init example --version react-native@latest options: {\\"cwd\\":\\"./react-native-alice-bobbi\\",\\"stdio\\":\\"inherit\\"}
",
"* ensureDir dir: react-native-alice-bobbi/scripts/
"* ensureDir dir: .../react-native-alice-bobbi/scripts/
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* outputFile name: react-native-alice-bobbi/scripts/examples_postinstall.js
"* outputFile name: .../react-native-alice-bobbi/scripts/examples_postinstall.js
content:
--------
#!/usr/bin/env node
Expand Down Expand Up @@ -911,7 +911,7 @@ content:

<<<<<<<< ======== >>>>>>>>
",
"* outputFile name: react-native-alice-bobbi/example/App.js
"* outputFile name: .../react-native-alice-bobbi/example/App.js
content:
--------
/**
Expand Down Expand Up @@ -976,11 +976,11 @@ const styles = StyleSheet.create({
",
Object {
"call": "fs.readFileSync",
"jsonFilePath": "./react-native-alice-bobbi/example/package.json",
"jsonFilePath": ".../react-native-alice-bobbi/example/package.json",
},
Object {
"call": "fs.writeFileSync",
"filePath": "./react-native-alice-bobbi/example/package.json",
"filePath": ".../react-native-alice-bobbi/example/package.json",
"json": "{
\\"name\\": \\"example\\",
\\"version\\": \\"0.0.1\\",
Expand All @@ -999,11 +999,11 @@ const styles = StyleSheet.create({
"spaces": 2,
},
},
"* execa.commandSync command: yarn add file:../ options: {\\"cwd\\":\\"./react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: yarn add file:../ options: {\\"cwd\\":\\".../react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod install options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod install options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const lib = require('../../../../../lib/lib.js');

const ioInject = require('../../../helpers/io-inject.js');

const mockcwd = require('process').cwd();

test('create alice-bobbi module with example, with `exampleFileLinkage: true` then recover from missing scripts in example package.json', async () => {
const mysnap = [];

Expand All @@ -13,7 +15,7 @@ test('create alice-bobbi module with example, with `exampleFileLinkage: true` th
readFileSync: (jsonFilePath) => {
mysnap.push({
call: 'fs.readFileSync',
jsonFilePath: jsonFilePath.replace(/\\/g, '/'),
jsonFilePath: jsonFilePath.replace(mockcwd, '...').replace(/\\/g, '/'),
});
return `{ "name": "example", "version": "0.0.1" }`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,11 @@ content:
",
"* execa.commandSync command: react-native init example --version react-native@latest options: {\\"cwd\\":\\"./react-native-alice-bobbi\\",\\"stdio\\":\\"inherit\\"}
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* ensureDir dir: react-native-alice-bobbi/example/
"* ensureDir dir: .../react-native-alice-bobbi/example/
",
"* outputFile name: react-native-alice-bobbi/example/metro.config.js
"* outputFile name: .../react-native-alice-bobbi/example/metro.config.js
content:
--------
// metro.config.js
Expand Down Expand Up @@ -828,7 +828,7 @@ module.exports = {

<<<<<<<< ======== >>>>>>>>
",
"* outputFile name: react-native-alice-bobbi/example/App.js
"* outputFile name: .../react-native-alice-bobbi/example/App.js
content:
--------
/**
Expand Down Expand Up @@ -891,11 +891,11 @@ const styles = StyleSheet.create({

<<<<<<<< ======== >>>>>>>>
",
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\"./react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: yarn add link:../ options: {\\"cwd\\":\\".../react-native-alice-bobbi/example\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod --version options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
"* execa.commandSync command: pod install options: {\\"cwd\\":\\"./react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
"* execa.commandSync command: pod install options: {\\"cwd\\":\\".../react-native-alice-bobbi/example/ios\\",\\"stdio\\":\\"inherit\\"}
",
]
`;
Loading