Added react-native new-library command

This commit is contained in:
Joe Stanton
2015-03-28 15:50:31 +00:00
parent b2d1d6f3e9
commit 396439bf86
11 changed files with 427 additions and 39 deletions

View File

@@ -10,6 +10,7 @@ var path = require('path');
var init = require('./init.js');
var install = require('./install.js');
var bundle = require('./bundle.js');
var newLibrary = require('./new-library.js');
function printUsage() {
console.log([
@@ -18,7 +19,8 @@ function printUsage() {
'Commands:',
' start: starts the webserver',
' install: installs npm react components',
' bundle: builds the javascript bundle for offline use'
' bundle: builds the javascript bundle for offline use',
' new-library: generates a native library bridge'
].join('\n'));
process.exit(1);
}
@@ -51,6 +53,9 @@ function run() {
case 'bundle':
bundle.init(args);
break;
case 'new-library':
newLibrary.init(args);
break;
case 'init':
printInitWarning();
break;

View File

@@ -0,0 +1,47 @@
'use strict';
var path = require('path');
var fs = require('fs');
function copyAndReplace(src, dest, replacements) {
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
} else {
var content = fs.readFileSync(src, 'utf8');
Object.keys(replacements).forEach(function(regex) {
content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
});
fs.writeFileSync(dest, content);
}
}
function walk(current) {
if (!fs.lstatSync(current).isDirectory()) {
return [current];
}
var files = fs.readdirSync(current).map(function(child) {
child = path.join(current, child);
return walk(child);
});
return [].concat.apply([current], files);
}
function validatePackageName(name) {
if (!name.match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
console.error(
'"%s" is not a valid name for a project. Please use a valid identifier ' +
'name (alphanumeric).',
name
);
process.exit(1);
}
}
module.exports = {
copyAndReplace: copyAndReplace,
walk: walk,
validatePackageName: validatePackageName
};

View File

@@ -1,15 +1,15 @@
'use strict';
var path = require('path');
var fs = require('fs');
var utils = require('./generator-utils');
function init(projectDir, appName) {
console.log('Setting up new React Native app in ' + projectDir);
var source = path.resolve(__dirname, '..', 'Examples/SampleApp');
walk(source).forEach(function(f) {
utils.walk(source).forEach(function(f) {
f = f.replace(source + '/', ''); // Strip off absolute path
if (f === 'project.xcworkspace' || f === 'xcuserdata') {
if (f === 'project.xcworkspace' || f.indexOf('.xcodeproj/xcuserdata') !== -1) {
return;
}
@@ -21,7 +21,7 @@ function init(projectDir, appName) {
};
var dest = f.replace(/SampleApp/g, appName).replace(/^_/, '.');
copyAndReplace(
utils.copyAndReplace(
path.resolve(source, f),
path.resolve(projectDir, dest),
replacements
@@ -34,30 +34,4 @@ function init(projectDir, appName) {
console.log('');
}
function copyAndReplace(src, dest, replacements) {
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
} else {
var content = fs.readFileSync(src, 'utf8');
Object.keys(replacements).forEach(function(regex) {
content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
});
fs.writeFileSync(dest, content);
}
}
function walk(current) {
if (!fs.lstatSync(current).isDirectory()) {
return [current];
}
var files = fs.readdirSync(current).map(function(child) {
child = path.join(current, child);
return walk(child);
});
return [].concat.apply([current], files);
}
module.exports = init;

59
local-cli/new-library.js Normal file
View File

@@ -0,0 +1,59 @@
'use strict';
var path = require('path');
var fs = require('fs');
var utils = require('./generator-utils');
function showHelp() {
console.log([
'Usage: react-native new-library <LibraryName>',
''
].join('\n'));
process.exit(1);
}
function newLibrary(libraryName) {
var root = process.cwd();
var libraries = path.resolve(root, 'Libraries');
var libraryDest = path.resolve(libraries, libraryName);
var source = path.resolve('node_modules', 'react-native', 'Libraries', 'Sample') + '/';
if (!fs.existsSync(libraries)) {
fs.mkdir(libraries);
}
if (fs.existsSync(libraryDest)) {
console.log('Library already exists in', libraryDest);
process.exit(1);
}
utils.walk(source).forEach(function(f) {
f = f.replace(source, ''); // Strip off absolute path
if (f === 'project.xcworkspace' || f.indexOf('.xcodeproj/xcuserdata') !== -1) {
return;
}
var dest = f.replace(/Sample/g, libraryName).replace(/^_/, '.');
utils.copyAndReplace(
path.resolve(source, f),
path.resolve(libraryDest, dest),
{ 'Sample': libraryName }
);
});
console.log('Created library in', libraryDest);
console.log('Next Steps:');
console.log(' Link your library in Xcode:');
console.log(' https://facebook.github.io/react-native/docs/linking-libraries.html#content');
console.log('');
}
module.exports = {
init: function(args) {
var libraryName = args[1];
if (!libraryName) { showHelp(); }
utils.validatePackageName(libraryName);
newLibrary(libraryName);
}
};