diff --git a/bin.js b/bin.js new file mode 100644 index 0000000..468d6c6 --- /dev/null +++ b/bin.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node + +// Yarn will fail to link workspace binaries if they haven't been built yet. Add +// a simple JS file to forward to the CLI which is built after install. +require('./src/cli'); diff --git a/package.json b/package.json new file mode 100644 index 0000000..a5cf76d --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "react-native-desktop-qt-init", + "version": "0.1.0", + "description": "CLI to bootstrap the addition of desktop-qt platform to existing react-native project", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "bin": { + "react-native-desktop-qt-init": "./bin.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/vkjr/react-native-desktop-qt-init.git" + }, + "author": "Volodymyr Kozieiev", + "license": "MIT", + "bugs": { + "url": "https://github.com/vkjr/react-native-desktop-qt-init/issues" + }, + "homepage": "https://github.com/vkjr/react-native-desktop-qt-init#readme", + "dependencies": { + "chalk": "^1.1.3" + } +} diff --git a/src/cli.js b/src/cli.js new file mode 100644 index 0000000..82a809f --- /dev/null +++ b/src/cli.js @@ -0,0 +1,50 @@ +'use strict'; + +const chalk = require('chalk'); +const Common = require('./common'); +const execSync = require('child_process').execSync; +const path = require('path'); + + +const PACKAGE = "git+https://github.com/status-im/react-native-desktop-qt.git#v060"; + +const REACT_NATIVE_DESKTOP_GENERATE_PATH = function() { + return path.resolve( + process.cwd(), + 'node_modules', + 'react-native-desktop-qt', + 'local-cli', + 'generate-desktop.js' + ); +}; + + +function installDesktopPackage() { + let rndPackage = PACKAGE; + + console.log(`Installing ${rndPackage}...`); + const pkgmgr = Common.isGlobalCliUsingYarn(process.cwd()) ? 'yarn add' : 'npm install --save'; + const execOptions = {}; // use {stdio: 'inherit'} for verbose + execSync(`${pkgmgr} ${rndPackage}`, execOptions); + console.log(chalk.green(`${rndPackage} successfully installed.`)); +} + +function runDesktopFilesGenerationScript() { + + const generateDesktop = require(REACT_NATIVE_DESKTOP_GENERATE_PATH()); + generateDesktop(process.cwd(), Common.getReactNativeAppName()); +} + + +(async () => { + + try { + installDesktopPackage(); + + runDesktopFilesGenerationScript(); + } catch (error) { + console.error(chalk.red(error.message)); + console.error(error); + process.exit(1); + } +})(); diff --git a/src/common.js b/src/common.js new file mode 100644 index 0000000..1a95a0d --- /dev/null +++ b/src/common.js @@ -0,0 +1,31 @@ + +'use strict'; + +const fs = require('fs'); +const path = require('path'); + + +const isGlobalCliUsingYarn = function(projectDir) { + return fs.existsSync(path.join(projectDir, 'yarn.lock')); +}; + + +const getReactNativeAppName = function () { + console.log('Reading application name from package.json...'); + let name = JSON.parse(fs.readFileSync('package.json', 'utf8')).name; + if (!name) { + if (fs.existsSync('app.json')) { + console.log('Reading application name from app.json...'); + name = JSON.parse(fs.readFileSync('app.json', 'utf8')).name; + } + } + if (!name) { + console.error('Please specify name in package.json or app.json'); + } + return name; +}; + +module.exports = { + isGlobalCliUsingYarn, + getReactNativeAppName, +};