2015-10-26 22:29:53 +00:00
|
|
|
/**
|
2015-10-16 15:20:29 +00:00
|
|
|
* 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 path = require('path');
|
|
|
|
const walk = require('../util/walk');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new native library with the given name
|
|
|
|
*/
|
2016-07-30 15:59:16 +00:00
|
|
|
function library(argv, config, args) {
|
2015-10-16 15:20:29 +00:00
|
|
|
if (!isValidPackageName(args.name)) {
|
2016-07-30 15:59:16 +00:00
|
|
|
return Promise.reject(
|
2015-10-16 15:20:29 +00:00
|
|
|
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)) {
|
2016-07-30 15:59:16 +00:00
|
|
|
return Promise.reject(`Library already exists in ${libraryDest}`);
|
2015-10-16 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
walk(source).forEach(f => {
|
|
|
|
if (f.indexOf('project.xcworkspace') !== -1 ||
|
|
|
|
f.indexOf('.xcodeproj/xcuserdata') !== -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-29 19:36:11 +00:00
|
|
|
const dest = path.relative(source, f.replace(/Sample/g, args.name).replace(/^_/, '.'));
|
2015-10-16 15:20:29 +00:00
|
|
|
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:');
|
2016-11-21 18:18:48 +00:00
|
|
|
console.log(
|
|
|
|
' https://facebook.github.io/react-native/docs/' +
|
|
|
|
'linking-libraries-ios.html#content\n'
|
|
|
|
);
|
2015-10-16 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
module.exports = {
|
|
|
|
name: 'new-library',
|
|
|
|
func: library,
|
|
|
|
description: 'generates a native library bridge',
|
|
|
|
options: [{
|
|
|
|
command: '--name <string>',
|
|
|
|
description: 'name of the library to generate',
|
|
|
|
default: null,
|
|
|
|
}],
|
|
|
|
};
|