[react-native-cli] Move `new-library` command to `private-cli`

This commit is contained in:
Martin Bigio 2015-10-16 11:20:29 -04:00
parent 4a3857ef1d
commit c0b5e6ed67
5 changed files with 151 additions and 2 deletions

View File

@ -9,13 +9,14 @@ var Config = require('../private-cli/src/util/Config');
var fs = require('fs');
var generate = require('../private-cli/src/generate/generate');
var init = require('./init.js');
var newLibrary = require('./new-library.js');
var library = require('../private-cli/src/library/library');
var runAndroid = require('./run-android.js');
var runPackager = require('./run-packager.js');
// TODO: remove once we fully roll out the `private-cli` based cli
// var bundle_DEPRECATED = require('./bundle.js');
// var generateAndroid_DEPRECATED = require('./generate-android.js');
// var newLibrary_DEPRECATED = require('./new-library.js');
function printUsage() {
console.log([
@ -55,7 +56,8 @@ function run() {
// bundle_DEPRECATED.init(args);
break;
case 'new-library':
newLibrary.init(args);
library(args, config).done();
// newLibrary_DEPRECATED.init(args);
break;
case 'init':
printInitWarning();

View File

@ -0,0 +1,77 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const copyAndReplace = require('../util/copyAndReplace');
const fs = require('fs');
const isValidPackageName = require('../util/isValidPackageName');
const parseCommandLine = require('../../../packager/parseCommandLine');
const path = require('path');
const Promise = require('promise');
const walk = require('../util/walk');
/**
* Creates a new native library with the given name
*/
function library(argv, config) {
return new Promise((resolve, reject) => {
_library(argv, config, resolve, reject);
});
}
function _library(argv, config, resolve, reject) {
const args = parseCommandLine([{
command: 'name',
description: 'Library name',
type: 'string',
required: true,
}], argv);
if (!isValidPackageName(args.name)) {
reject(
args.name + ' is not a valid name for a project. Please use a valid ' +
'identifier name (alphanumeric).'
);
}
const root = process.cwd();
const libraries = path.resolve(root, 'Libraries');
const libraryDest = path.resolve(libraries, args.name);
const source = path.resolve('node_modules', 'react-native', 'Libraries', 'Sample');
if (!fs.existsSync(libraries)) {
fs.mkdir(libraries);
}
if (fs.existsSync(libraryDest)) {
reject('Library already exists in', libraryDest);
}
walk(source).forEach(f => {
if (f.indexOf('project.xcworkspace') !== -1 ||
f.indexOf('.xcodeproj/xcuserdata') !== -1) {
return;
}
const dest = f.replace(/Sample/g, args.name).replace(/^_/, '.');
copyAndReplace(
path.resolve(source, f),
path.resolve(libraryDest, dest),
{'Sample': args.name}
);
});
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-ios.html#content\n');
resolve();
}
module.exports = library;

View File

@ -0,0 +1,29 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
function copyAndReplace(src, dest, replacements) {
console.log('src', src);
console.log('dest', dest);
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
} else {
let content = fs.readFileSync(src, 'utf8');
Object.keys(replacements).forEach(regex =>
content = content.replace(new RegExp(regex, 'g'), replacements[regex])
);
fs.writeFileSync(dest, content);
}
}
module.exports = copyAndReplace;

View File

@ -0,0 +1,15 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
function isValidPackageName(name) {
return name.match(/^[$A-Z_][0-9A-Z_$]*$/i);
}
module.exports = isValidPackageName;

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
const path = require('path');
function walk(current) {
if (!fs.lstatSync(current).isDirectory()) {
return [current];
}
const files = fs.readdirSync(current).map(child => {
child = path.join(current, child);
return walk(child);
});
return [].concat.apply([current], files);
}
module.exports = walk;