2018-05-11 19:43:49 +00:00
|
|
|
/**
|
2015-10-16 15:20:29 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2015-10-16 15:20:29 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
2015-10-16 15:20:29 +00:00
|
|
|
'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(
|
2018-05-11 19:43:49 +00:00
|
|
|
args.name +
|
|
|
|
' is not a valid name for a project. Please use a valid ' +
|
|
|
|
'identifier name (alphanumeric).',
|
2015-10-16 15:20:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = process.cwd();
|
|
|
|
const libraries = path.resolve(root, 'Libraries');
|
|
|
|
const libraryDest = path.resolve(libraries, args.name);
|
2018-05-11 19:43:49 +00:00
|
|
|
const source = path.resolve(
|
|
|
|
'node_modules',
|
|
|
|
'react-native',
|
|
|
|
'Libraries',
|
|
|
|
'Sample',
|
|
|
|
);
|
2015-10-16 15:20:29 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(libraries)) {
|
2018-02-26 05:40:17 +00:00
|
|
|
fs.mkdirSync(libraries);
|
2015-10-16 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.existsSync(libraryDest)) {
|
2018-05-11 19:43:49 +00:00
|
|
|
return Promise.reject(
|
|
|
|
new Error(`Library already exists in ${libraryDest}`),
|
|
|
|
);
|
2015-10-16 15:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
walk(source).forEach(f => {
|
2018-05-11 19:43:49 +00:00
|
|
|
if (
|
|
|
|
f.indexOf('project.xcworkspace') !== -1 ||
|
|
|
|
f.indexOf('.xcodeproj/xcuserdata') !== -1
|
|
|
|
) {
|
2015-10-16 15:20:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const dest = path.relative(
|
|
|
|
source,
|
|
|
|
f.replace(/Sample/g, args.name).replace(/^_/, '.'),
|
2015-10-16 15:20:29 +00:00
|
|
|
);
|
2018-05-11 19:43:49 +00:00
|
|
|
copyAndReplace(path.resolve(source, f), path.resolve(libraryDest, dest), {
|
|
|
|
Sample: args.name,
|
|
|
|
});
|
2015-10-16 15:20:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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/' +
|
2018-05-11 19:43:49 +00:00
|
|
|
'linking-libraries-ios.html#content\n',
|
2016-11-21 18:18:48 +00:00
|
|
|
);
|
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',
|
2018-05-11 19:43:49 +00:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
command: '--name <string>',
|
|
|
|
description: 'name of the library to generate',
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
],
|
2016-07-30 15:59:16 +00:00
|
|
|
};
|