-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathlib.js
142 lines (126 loc) · 4.72 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const path = require('path');
const pascalCase = require('pascal-case');
const paramCase = require('param-case');
const templates = require('./templates');
const { hasPrefix, createFile, createFolder, npmAddScriptSync, exec } = require('./utils');
const { execSync } = require('child_process');
const DEFAULT_NAME = 'Library';
const DEFAULT_PREFIX = 'RN';
const DEFAULT_MODULE_PREFIX = 'react-native';
const DEFAULT_PACKAGE_IDENTIFIER = 'com.reactlibrary';
const DEFAULT_PLATFORMS = ['android', 'ios', 'windows'];
const DEFAULT_OVERRIDE_PREFIX = false;
const DEFAULT_GITHUB_ACCOUNT = 'github_account';
const DEFAULT_AUTHOR_NAME = 'Your Name';
const DEFAULT_AUTHOR_EMAIL = '[email protected]';
const DEFAULT_LICENSE = 'Apache-2.0';
const DEFAULT_GENERATE_EXAMPLE = false;
const renderTemplate = (name, template, templateArgs) => {
const filename = path.join(name, template.name(templateArgs));
const baseDir = filename.split(path.basename(filename))[0];
return createFolder(baseDir).then(() =>
createFile(filename, template.content(templateArgs))
);
}
module.exports = ({
namespace,
name = DEFAULT_NAME,
prefix = DEFAULT_PREFIX,
modulePrefix = DEFAULT_MODULE_PREFIX,
packageIdentifier = DEFAULT_PACKAGE_IDENTIFIER,
platforms = DEFAULT_PLATFORMS,
overridePrefix = DEFAULT_OVERRIDE_PREFIX,
githubAccount = DEFAULT_GITHUB_ACCOUNT,
authorName = DEFAULT_AUTHOR_NAME,
authorEmail = DEFAULT_AUTHOR_EMAIL,
license = DEFAULT_LICENSE,
generateExample = DEFAULT_GENERATE_EXAMPLE,
}) => {
if (!overridePrefix) {
if (hasPrefix(name)) {
throw new Error('Please don\'t include the prefix in the name');
}
if (prefix === 'RCT') {
throw new Error(`The \`RCT\` name prefix is reserved for core React modules.
Please use a different prefix.`);
}
}
if (platforms.length === 0) {
throw new Error('Please specify at least one platform to generate the library.');
}
if (prefix === DEFAULT_PREFIX) {
console.warn(`While \`${DEFAULT_PREFIX}\` is the default prefix,
it is recommended to customize the prefix.`);
}
if (packageIdentifier === DEFAULT_PACKAGE_IDENTIFIER) {
console.warn(`While \`{DEFAULT_PACKAGE_IDENTIFIER}\` is the default package
identifier, it is recommended to customize the package identifier.`);
}
const moduleName = `${modulePrefix}-${paramCase(name)}`;
const rootFolderName = moduleName;
return createFolder(rootFolderName)
.then(() => {
return Promise.all(templates.filter((template) => {
if (template.platform) {
return (platforms.indexOf(template.platform) >= 0);
}
return true;
}).map((template) => {
if (!template.name) {
return Promise.resolve();
}
const templateArgs = {
name: `${prefix}${pascalCase(name)}`,
moduleName,
packageIdentifier,
namespace: namespace || pascalCase(name).split(/(?=[A-Z])/).join('.'),
platforms,
githubAccount,
authorName,
authorEmail,
license,
generateExample,
};
return renderTemplate(rootFolderName, template, templateArgs);
}));
})
.then(() => {
// Generate the example if necessary
if (!generateExample) {
return Promise.resolve();
}
const initExampleOptions = { cwd: `./${rootFolderName}`, stdio: 'inherit' };
return exec('react-native init example', initExampleOptions)
.then(() => {
// Execute the example template
const exampleTemplates = require('./templates/example');
return Promise.all(
exampleTemplates.map((template) => {
return renderTemplate(rootFolderName, template);
})
);
})
.then(() => {
// Adds and link the new library
return new Promise((resolve, reject) => {
// Add postinstall script to example package.json
const pathExampleApp = `./${rootFolderName}/example`;
const moduleName = `${modulePrefix}-${paramCase(name)}`;
npmAddScriptSync(`${pathExampleApp}/package.json`, {
key: 'postinstall',
value: `node ../scripts/examples_postinstall.js node_modules/${moduleName}`
});
// Add and link the new library
const addLinkLibraryOptions = { cwd: pathExampleApp, stdio: 'inherit' };
try {
execSync('yarn add file:../', addLinkLibraryOptions);
} catch (e) {
execSync('npm install ../', addLinkLibraryOptions);
execSync('npm install', addLinkLibraryOptions);
}
execSync('react-native link', addLinkLibraryOptions);
return resolve();
});
});
});
};