mirror of
https://github.com/status-im/react-native.git
synced 2025-01-26 09:19:10 +00:00
[react-native-cli] Move new-library
command to private-cli
This commit is contained in:
parent
4a3857ef1d
commit
c0b5e6ed67
@ -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();
|
||||
|
77
private-cli/src/library/library.js
Normal file
77
private-cli/src/library/library.js
Normal 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;
|
29
private-cli/src/util/copyAndReplace.js
Normal file
29
private-cli/src/util/copyAndReplace.js
Normal 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;
|
15
private-cli/src/util/isValidPackageName.js
Normal file
15
private-cli/src/util/isValidPackageName.js
Normal 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;
|
26
private-cli/src/util/walk.js
Normal file
26
private-cli/src/util/walk.js
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user