create-expo-module 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js ADDED
@@ -0,0 +1,2 @@
1
+ // @generated by expo-module-scripts
2
+ module.exports = require('expo-module-scripts/eslintrc.base.js');
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # create-expo-module
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../build/create-expo-module.js')
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const spawn_async_1 = __importDefault(require("@expo/spawn-async"));
7
+ const commander_1 = require("commander");
8
+ const download_tarball_1 = __importDefault(require("download-tarball"));
9
+ const ejs_1 = __importDefault(require("ejs"));
10
+ const fs_extra_1 = __importDefault(require("fs-extra"));
11
+ const path_1 = __importDefault(require("path"));
12
+ const prompts_1 = __importDefault(require("prompts"));
13
+ const packageJson = require('../package.json');
14
+ // `yarn run` may change the current working dir, then we should use `INIT_CWD` env.
15
+ const CWD = process.env.INIT_CWD || process.cwd();
16
+ // Ignore some paths. Especially `package.json` as it is rendered
17
+ // from `$package.json` file instead of the original one.
18
+ const IGNORES_PATHS = ['.DS_Store', 'build', 'node_modules', 'package.json'];
19
+ /**
20
+ * The main function of the command.
21
+ *
22
+ * @param target Path to the directory where to create the module. Defaults to current working dir.
23
+ * @param command An object from `commander`.
24
+ */
25
+ async function main(target, options) {
26
+ const targetDir = target ? path_1.default.join(CWD, target) : CWD;
27
+ options.target = targetDir;
28
+ await fs_extra_1.default.ensureDir(targetDir);
29
+ const packagePath = options.source
30
+ ? path_1.default.join(CWD, options.source)
31
+ : await downloadPackageAsync(targetDir);
32
+ const files = await getFilesAsync(packagePath);
33
+ const data = await askForSubstitutionDataAsync(targetDir, options);
34
+ // Iterate through all template files.
35
+ for (const file of files) {
36
+ const renderedRelativePath = ejs_1.default.render(file.replace(/^\$/, ''), data, {
37
+ openDelimiter: '{',
38
+ closeDelimiter: '}',
39
+ escape: (value) => value.replace('.', path_1.default.sep),
40
+ });
41
+ const fromPath = path_1.default.join(packagePath, file);
42
+ const toPath = path_1.default.join(targetDir, renderedRelativePath);
43
+ const template = await fs_extra_1.default.readFile(fromPath, { encoding: 'utf8' });
44
+ const renderedContent = ejs_1.default.render(template, data);
45
+ await fs_extra_1.default.outputFile(toPath, renderedContent, { encoding: 'utf8' });
46
+ }
47
+ if (!options.source) {
48
+ // Files in the downloaded tarball are wrapped in `package` dir.
49
+ // We should remove it after all.
50
+ await fs_extra_1.default.remove(packagePath);
51
+ }
52
+ if (!options.withReadme) {
53
+ await fs_extra_1.default.remove(path_1.default.join(targetDir, 'README.md'));
54
+ }
55
+ if (!options.withChangelog) {
56
+ await fs_extra_1.default.remove(path_1.default.join(targetDir, 'CHANGELOG.md'));
57
+ }
58
+ // Build TypeScript files.
59
+ await (0, spawn_async_1.default)('npm', ['run', 'build'], {
60
+ cwd: targetDir,
61
+ });
62
+ }
63
+ /**
64
+ * Recursively scans for the files within the directory. Returned paths are relative to the `root` path.
65
+ */
66
+ async function getFilesAsync(root, dir = null) {
67
+ const files = [];
68
+ const baseDir = dir ? path_1.default.join(root, dir) : root;
69
+ for (const file of await fs_extra_1.default.readdir(baseDir)) {
70
+ const relativePath = dir ? path_1.default.join(dir, file) : file;
71
+ if (IGNORES_PATHS.includes(relativePath) || IGNORES_PATHS.includes(file)) {
72
+ continue;
73
+ }
74
+ const fullPath = path_1.default.join(baseDir, file);
75
+ const stat = await fs_extra_1.default.lstat(fullPath);
76
+ if (stat.isDirectory()) {
77
+ files.push(...(await getFilesAsync(root, relativePath)));
78
+ }
79
+ else {
80
+ files.push(relativePath);
81
+ }
82
+ }
83
+ return files;
84
+ }
85
+ /**
86
+ * Asks NPM registry for the url to the tarball.
87
+ */
88
+ async function getNpmTarballUrl(packageName, version = 'latest') {
89
+ const { stdout } = await (0, spawn_async_1.default)('npm', ['view', `${packageName}@${version}`, 'dist.tarball']);
90
+ return stdout.trim();
91
+ }
92
+ /**
93
+ * Gets the username of currently logged in user. Used as a default in the prompt asking for the module author.
94
+ */
95
+ async function npmWhoamiAsync(targetDir) {
96
+ try {
97
+ const { stdout } = await (0, spawn_async_1.default)('npm', ['whoami'], { cwd: targetDir });
98
+ return stdout.trim();
99
+ }
100
+ catch (e) {
101
+ return null;
102
+ }
103
+ }
104
+ /**
105
+ * Downloads the template from NPM registry.
106
+ */
107
+ async function downloadPackageAsync(targetDir) {
108
+ const tarballUrl = await getNpmTarballUrl('expo-module-template');
109
+ await (0, download_tarball_1.default)({
110
+ url: tarballUrl,
111
+ dir: targetDir,
112
+ });
113
+ return path_1.default.join(targetDir, 'package');
114
+ }
115
+ /**
116
+ * Asks the user for some data necessary to render the template.
117
+ * Some values may already be provided by command options, the prompt is skipped in that case.
118
+ */
119
+ async function askForSubstitutionDataAsync(targetDir, options) {
120
+ var _a, _b;
121
+ const defaultPackageSlug = path_1.default.basename(targetDir);
122
+ const defaultProjectName = defaultPackageSlug
123
+ .replace(/^./, (match) => match.toUpperCase())
124
+ .replace(/\W+(\w)/g, (_, p1) => p1.toUpperCase());
125
+ const promptQueries = [
126
+ {
127
+ type: 'text',
128
+ name: 'slug',
129
+ message: 'What is the package slug?',
130
+ initial: defaultPackageSlug,
131
+ resolvedValue: options.target ? defaultPackageSlug : null,
132
+ },
133
+ {
134
+ type: 'text',
135
+ name: 'name',
136
+ message: 'What is the project name?',
137
+ initial: defaultProjectName,
138
+ },
139
+ {
140
+ type: 'text',
141
+ name: 'description',
142
+ message: 'How would you describe the module?',
143
+ },
144
+ {
145
+ type: 'text',
146
+ name: 'package',
147
+ message: 'What is the Android package name?',
148
+ initial: `expo.modules.${defaultPackageSlug.replace(/\W/g, '').toLowerCase()}`,
149
+ },
150
+ {
151
+ type: 'text',
152
+ name: 'author',
153
+ message: 'Who is the author?',
154
+ initial: (_a = (await npmWhoamiAsync(targetDir))) !== null && _a !== void 0 ? _a : '',
155
+ },
156
+ {
157
+ type: 'text',
158
+ name: 'license',
159
+ message: 'What is the license?',
160
+ initial: 'MIT',
161
+ },
162
+ {
163
+ type: 'text',
164
+ name: 'repo',
165
+ message: 'What is the repository URL?',
166
+ },
167
+ ];
168
+ const answers = {};
169
+ for (const query of promptQueries) {
170
+ const { name, resolvedValue } = query;
171
+ answers[name] = (_b = resolvedValue !== null && resolvedValue !== void 0 ? resolvedValue : options[name]) !== null && _b !== void 0 ? _b : (await (0, prompts_1.default)(query))[name];
172
+ }
173
+ const { slug, name, description, package: projectPackage, author, license, repo } = answers;
174
+ return {
175
+ project: {
176
+ slug,
177
+ name,
178
+ version: '0.1.0',
179
+ description,
180
+ package: projectPackage,
181
+ },
182
+ author,
183
+ license,
184
+ repo,
185
+ };
186
+ }
187
+ const program = new commander_1.Command();
188
+ program
189
+ .name(packageJson.name)
190
+ .version(packageJson.version)
191
+ .description(packageJson.description)
192
+ .arguments('[target_dir]')
193
+ .option('-s, --source <source_dir>', 'Local path to the template. By default it downloads `expo-module-template` from NPM.')
194
+ .option('-n, --name <module_name>', 'Name of the native module.')
195
+ .option('-d, --description <description>', 'Description of the module.')
196
+ .option('-p, --package <package>', 'The Android package name.')
197
+ .option('-a, --author <author>', 'The author name.')
198
+ .option('-l, --license <license>', 'The license that the module is distributed with.')
199
+ .option('-r, --repo <repo_url>', 'The URL to the repository.')
200
+ .option('--with-readme', 'Whether to include README.md file.', false)
201
+ .option('--with-changelog', 'Whether to include CHANGELOG.md file.', false)
202
+ .action(main);
203
+ program.parse(process.argv);
204
+ //# sourceMappingURL=create-expo-module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-expo-module.js","sourceRoot":"","sources":["../src/create-expo-module.ts"],"names":[],"mappings":";;;;;AAAA,oEAA2C;AAC3C,yCAAoC;AACpC,wEAA+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,MAAM,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAC;AAE9B,OAAO;KACJ,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;KACtB,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC;KAC5B,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC;KACpC,SAAS,CAAC,cAAc,CAAC;KACzB,MAAM,CACL,2BAA2B,EAC3B,sFAAsF,CACvF;KACA,MAAM,CAAC,0BAA0B,EAAE,4BAA4B,CAAC;KAChE,MAAM,CAAC,iCAAiC,EAAE,4BAA4B,CAAC;KACvE,MAAM,CAAC,yBAAyB,EAAE,2BAA2B,CAAC;KAC9D,MAAM,CAAC,uBAAuB,EAAE,kBAAkB,CAAC;KACnD,MAAM,CAAC,yBAAyB,EAAE,kDAAkD,CAAC;KACrF,MAAM,CAAC,uBAAuB,EAAE,4BAA4B,CAAC;KAC7D,MAAM,CAAC,eAAe,EAAE,oCAAoC,EAAE,KAAK,CAAC;KACpE,MAAM,CAAC,kBAAkB,EAAE,uCAAuC,EAAE,KAAK,CAAC;KAC1E,MAAM,CAAC,IAAI,CAAC,CAAC;AAEhB,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC","sourcesContent":["import spawnAsync from '@expo/spawn-async';\nimport { Command } from 'commander';\nimport downloadTarball from 'download-tarball';\nimport ejs from 'ejs';\nimport fs from 'fs-extra';\nimport path from 'path';\nimport prompts, { PromptObject } from 'prompts';\n\nconst packageJson = require('../package.json');\n\n// `yarn run` may change the current working dir, then we should use `INIT_CWD` env.\nconst CWD = process.env.INIT_CWD || process.cwd();\n\n// Ignore some paths. Especially `package.json` as it is rendered\n// from `$package.json` file instead of the original one.\nconst IGNORES_PATHS = ['.DS_Store', 'build', 'node_modules', 'package.json'];\n\n/**\n * Possible command options.\n */\ntype CommandOptions = {\n target: string;\n source?: string;\n name?: string;\n description?: string;\n package?: string;\n author?: string;\n license?: string;\n repo?: string;\n withReadme: boolean;\n withChangelog: boolean;\n};\n\n/**\n * Represents an object that is passed to `ejs` when rendering the template.\n */\ntype SubstitutionData = {\n project: {\n slug: string;\n name: string;\n version: string;\n description: string;\n package: string;\n };\n author: string;\n license: string;\n repo: string;\n};\n\ntype CustomPromptObject = PromptObject & {\n name: string;\n resolvedValue?: string | null;\n};\n\n/**\n * The main function of the command.\n *\n * @param target Path to the directory where to create the module. Defaults to current working dir.\n * @param command An object from `commander`.\n */\nasync function main(target: string | undefined, options: CommandOptions) {\n const targetDir = target ? path.join(CWD, target) : CWD;\n\n options.target = targetDir;\n await fs.ensureDir(targetDir);\n\n const packagePath = options.source\n ? path.join(CWD, options.source)\n : await downloadPackageAsync(targetDir);\n const files = await getFilesAsync(packagePath);\n const data = await askForSubstitutionDataAsync(targetDir, options);\n\n // Iterate through all template files.\n for (const file of files) {\n const renderedRelativePath = ejs.render(file.replace(/^\\$/, ''), data, {\n openDelimiter: '{',\n closeDelimiter: '}',\n escape: (value: string) => value.replace('.', path.sep),\n });\n const fromPath = path.join(packagePath, file);\n const toPath = path.join(targetDir, renderedRelativePath);\n const template = await fs.readFile(fromPath, { encoding: 'utf8' });\n const renderedContent = ejs.render(template, data);\n\n await fs.outputFile(toPath, renderedContent, { encoding: 'utf8' });\n }\n\n if (!options.source) {\n // Files in the downloaded tarball are wrapped in `package` dir.\n // We should remove it after all.\n await fs.remove(packagePath);\n }\n if (!options.withReadme) {\n await fs.remove(path.join(targetDir, 'README.md'));\n }\n if (!options.withChangelog) {\n await fs.remove(path.join(targetDir, 'CHANGELOG.md'));\n }\n\n // Build TypeScript files.\n await spawnAsync('npm', ['run', 'build'], {\n cwd: targetDir,\n });\n}\n\n/**\n * Recursively scans for the files within the directory. Returned paths are relative to the `root` path.\n */\nasync function getFilesAsync(root: string, dir: string | null = null): Promise<string[]> {\n const files: string[] = [];\n const baseDir = dir ? path.join(root, dir) : root;\n\n for (const file of await fs.readdir(baseDir)) {\n const relativePath = dir ? path.join(dir, file) : file;\n\n if (IGNORES_PATHS.includes(relativePath) || IGNORES_PATHS.includes(file)) {\n continue;\n }\n\n const fullPath = path.join(baseDir, file);\n const stat = await fs.lstat(fullPath);\n\n if (stat.isDirectory()) {\n files.push(...(await getFilesAsync(root, relativePath)));\n } else {\n files.push(relativePath);\n }\n }\n return files;\n}\n\n/**\n * Asks NPM registry for the url to the tarball.\n */\nasync function getNpmTarballUrl(packageName: string, version: string = 'latest'): Promise<string> {\n const { stdout } = await spawnAsync('npm', ['view', `${packageName}@${version}`, 'dist.tarball']);\n return stdout.trim();\n}\n\n/**\n * Gets the username of currently logged in user. Used as a default in the prompt asking for the module author.\n */\nasync function npmWhoamiAsync(targetDir: string): Promise<string | null> {\n try {\n const { stdout } = await spawnAsync('npm', ['whoami'], { cwd: targetDir });\n return stdout.trim();\n } catch (e) {\n return null;\n }\n}\n\n/**\n * Downloads the template from NPM registry.\n */\nasync function downloadPackageAsync(targetDir: string): Promise<string> {\n const tarballUrl = await getNpmTarballUrl('expo-module-template');\n\n await downloadTarball({\n url: tarballUrl,\n dir: targetDir,\n });\n return path.join(targetDir, 'package');\n}\n\n/**\n * Asks the user for some data necessary to render the template.\n * Some values may already be provided by command options, the prompt is skipped in that case.\n */\nasync function askForSubstitutionDataAsync(\n targetDir: string,\n options: CommandOptions\n): Promise<SubstitutionData> {\n const defaultPackageSlug = path.basename(targetDir);\n const defaultProjectName = defaultPackageSlug\n .replace(/^./, (match) => match.toUpperCase())\n .replace(/\\W+(\\w)/g, (_, p1) => p1.toUpperCase());\n\n const promptQueries: CustomPromptObject[] = [\n {\n type: 'text',\n name: 'slug',\n message: 'What is the package slug?',\n initial: defaultPackageSlug,\n resolvedValue: options.target ? defaultPackageSlug : null,\n },\n {\n type: 'text',\n name: 'name',\n message: 'What is the project name?',\n initial: defaultProjectName,\n },\n {\n type: 'text',\n name: 'description',\n message: 'How would you describe the module?',\n },\n {\n type: 'text',\n name: 'package',\n message: 'What is the Android package name?',\n initial: `expo.modules.${defaultPackageSlug.replace(/\\W/g, '').toLowerCase()}`,\n },\n {\n type: 'text',\n name: 'author',\n message: 'Who is the author?',\n initial: (await npmWhoamiAsync(targetDir)) ?? '',\n },\n {\n type: 'text',\n name: 'license',\n message: 'What is the license?',\n initial: 'MIT',\n },\n {\n type: 'text',\n name: 'repo',\n message: 'What is the repository URL?',\n },\n ];\n\n const answers: Record<string, string> = {};\n for (const query of promptQueries) {\n const { name, resolvedValue } = query;\n answers[name] = resolvedValue ?? options[name] ?? (await prompts(query))[name];\n }\n\n const { slug, name, description, package: projectPackage, author, license, repo } = answers;\n\n return {\n project: {\n slug,\n name,\n version: '0.1.0',\n description,\n package: projectPackage,\n },\n author,\n license,\n repo,\n };\n}\n\nconst program = new Command();\n\nprogram\n .name(packageJson.name)\n .version(packageJson.version)\n .description(packageJson.description)\n .arguments('[target_dir]')\n .option(\n '-s, --source <source_dir>',\n 'Local path to the template. By default it downloads `expo-module-template` from NPM.'\n )\n .option('-n, --name <module_name>', 'Name of the native module.')\n .option('-d, --description <description>', 'Description of the module.')\n .option('-p, --package <package>', 'The Android package name.')\n .option('-a, --author <author>', 'The author name.')\n .option('-l, --license <license>', 'The license that the module is distributed with.')\n .option('-r, --repo <repo_url>', 'The URL to the repository.')\n .option('--with-readme', 'Whether to include README.md file.', false)\n .option('--with-changelog', 'Whether to include CHANGELOG.md file.', false)\n .action(main);\n\nprogram.parse(process.argv);\n"]}
package/package.json CHANGED
@@ -1,11 +1,49 @@
1
1
  {
2
2
  "name": "create-expo-module",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.1.0",
4
+ "description": "The script to create the Expo module",
5
+ "main": "build/create-expo-module.js",
6
+ "types": "build/create-expo-module.d.ts",
6
7
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ "build": "expo-module build",
9
+ "clean": "expo-module clean",
10
+ "lint": "expo-module lint",
11
+ "expo-module": "expo-module"
8
12
  },
9
- "author": "",
10
- "license": "MIT"
13
+ "bin": {
14
+ "create-expo-module": "./bin/create-expo-module"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/expo/expo.git",
19
+ "directory": "packages/create-expo-module"
20
+ },
21
+ "keywords": [
22
+ "expo",
23
+ "module",
24
+ "modules",
25
+ "library",
26
+ "react",
27
+ "native"
28
+ ],
29
+ "author": "Expo",
30
+ "license": "MIT",
31
+ "bugs": {
32
+ "url": "https://github.com/expo/expo/issues"
33
+ },
34
+ "homepage": "https://github.com/expo/expo/tree/master/packages/expo",
35
+ "dependencies": {
36
+ "@expo/spawn-async": "^1.5.0",
37
+ "commander": "^8.3.0",
38
+ "download-tarball": "^2.0.0",
39
+ "ejs": "^3.1.6",
40
+ "fs-extra": "^10.0.0",
41
+ "prompts": "^2.4.2"
42
+ },
43
+ "devDependencies": {
44
+ "@types/ejs": "^3.1.0",
45
+ "@types/prompts": "^2.0.14",
46
+ "expo-module-scripts": "^2.0.0"
47
+ },
48
+ "gitHead": "c87080dbe1d7cefd66a9f959c090ff1a2d2ab26d"
11
49
  }
@@ -0,0 +1,265 @@
1
+ import spawnAsync from '@expo/spawn-async';
2
+ import { Command } from 'commander';
3
+ import downloadTarball from 'download-tarball';
4
+ import ejs from 'ejs';
5
+ import fs from 'fs-extra';
6
+ import path from 'path';
7
+ import prompts, { PromptObject } from 'prompts';
8
+
9
+ const packageJson = require('../package.json');
10
+
11
+ // `yarn run` may change the current working dir, then we should use `INIT_CWD` env.
12
+ const CWD = process.env.INIT_CWD || process.cwd();
13
+
14
+ // Ignore some paths. Especially `package.json` as it is rendered
15
+ // from `$package.json` file instead of the original one.
16
+ const IGNORES_PATHS = ['.DS_Store', 'build', 'node_modules', 'package.json'];
17
+
18
+ /**
19
+ * Possible command options.
20
+ */
21
+ type CommandOptions = {
22
+ target: string;
23
+ source?: string;
24
+ name?: string;
25
+ description?: string;
26
+ package?: string;
27
+ author?: string;
28
+ license?: string;
29
+ repo?: string;
30
+ withReadme: boolean;
31
+ withChangelog: boolean;
32
+ };
33
+
34
+ /**
35
+ * Represents an object that is passed to `ejs` when rendering the template.
36
+ */
37
+ type SubstitutionData = {
38
+ project: {
39
+ slug: string;
40
+ name: string;
41
+ version: string;
42
+ description: string;
43
+ package: string;
44
+ };
45
+ author: string;
46
+ license: string;
47
+ repo: string;
48
+ };
49
+
50
+ type CustomPromptObject = PromptObject & {
51
+ name: string;
52
+ resolvedValue?: string | null;
53
+ };
54
+
55
+ /**
56
+ * The main function of the command.
57
+ *
58
+ * @param target Path to the directory where to create the module. Defaults to current working dir.
59
+ * @param command An object from `commander`.
60
+ */
61
+ async function main(target: string | undefined, options: CommandOptions) {
62
+ const targetDir = target ? path.join(CWD, target) : CWD;
63
+
64
+ options.target = targetDir;
65
+ await fs.ensureDir(targetDir);
66
+
67
+ const packagePath = options.source
68
+ ? path.join(CWD, options.source)
69
+ : await downloadPackageAsync(targetDir);
70
+ const files = await getFilesAsync(packagePath);
71
+ const data = await askForSubstitutionDataAsync(targetDir, options);
72
+
73
+ // Iterate through all template files.
74
+ for (const file of files) {
75
+ const renderedRelativePath = ejs.render(file.replace(/^\$/, ''), data, {
76
+ openDelimiter: '{',
77
+ closeDelimiter: '}',
78
+ escape: (value: string) => value.replace('.', path.sep),
79
+ });
80
+ const fromPath = path.join(packagePath, file);
81
+ const toPath = path.join(targetDir, renderedRelativePath);
82
+ const template = await fs.readFile(fromPath, { encoding: 'utf8' });
83
+ const renderedContent = ejs.render(template, data);
84
+
85
+ await fs.outputFile(toPath, renderedContent, { encoding: 'utf8' });
86
+ }
87
+
88
+ if (!options.source) {
89
+ // Files in the downloaded tarball are wrapped in `package` dir.
90
+ // We should remove it after all.
91
+ await fs.remove(packagePath);
92
+ }
93
+ if (!options.withReadme) {
94
+ await fs.remove(path.join(targetDir, 'README.md'));
95
+ }
96
+ if (!options.withChangelog) {
97
+ await fs.remove(path.join(targetDir, 'CHANGELOG.md'));
98
+ }
99
+
100
+ // Build TypeScript files.
101
+ await spawnAsync('npm', ['run', 'build'], {
102
+ cwd: targetDir,
103
+ });
104
+ }
105
+
106
+ /**
107
+ * Recursively scans for the files within the directory. Returned paths are relative to the `root` path.
108
+ */
109
+ async function getFilesAsync(root: string, dir: string | null = null): Promise<string[]> {
110
+ const files: string[] = [];
111
+ const baseDir = dir ? path.join(root, dir) : root;
112
+
113
+ for (const file of await fs.readdir(baseDir)) {
114
+ const relativePath = dir ? path.join(dir, file) : file;
115
+
116
+ if (IGNORES_PATHS.includes(relativePath) || IGNORES_PATHS.includes(file)) {
117
+ continue;
118
+ }
119
+
120
+ const fullPath = path.join(baseDir, file);
121
+ const stat = await fs.lstat(fullPath);
122
+
123
+ if (stat.isDirectory()) {
124
+ files.push(...(await getFilesAsync(root, relativePath)));
125
+ } else {
126
+ files.push(relativePath);
127
+ }
128
+ }
129
+ return files;
130
+ }
131
+
132
+ /**
133
+ * Asks NPM registry for the url to the tarball.
134
+ */
135
+ async function getNpmTarballUrl(packageName: string, version: string = 'latest'): Promise<string> {
136
+ const { stdout } = await spawnAsync('npm', ['view', `${packageName}@${version}`, 'dist.tarball']);
137
+ return stdout.trim();
138
+ }
139
+
140
+ /**
141
+ * Gets the username of currently logged in user. Used as a default in the prompt asking for the module author.
142
+ */
143
+ async function npmWhoamiAsync(targetDir: string): Promise<string | null> {
144
+ try {
145
+ const { stdout } = await spawnAsync('npm', ['whoami'], { cwd: targetDir });
146
+ return stdout.trim();
147
+ } catch (e) {
148
+ return null;
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Downloads the template from NPM registry.
154
+ */
155
+ async function downloadPackageAsync(targetDir: string): Promise<string> {
156
+ const tarballUrl = await getNpmTarballUrl('expo-module-template');
157
+
158
+ await downloadTarball({
159
+ url: tarballUrl,
160
+ dir: targetDir,
161
+ });
162
+ return path.join(targetDir, 'package');
163
+ }
164
+
165
+ /**
166
+ * Asks the user for some data necessary to render the template.
167
+ * Some values may already be provided by command options, the prompt is skipped in that case.
168
+ */
169
+ async function askForSubstitutionDataAsync(
170
+ targetDir: string,
171
+ options: CommandOptions
172
+ ): Promise<SubstitutionData> {
173
+ const defaultPackageSlug = path.basename(targetDir);
174
+ const defaultProjectName = defaultPackageSlug
175
+ .replace(/^./, (match) => match.toUpperCase())
176
+ .replace(/\W+(\w)/g, (_, p1) => p1.toUpperCase());
177
+
178
+ const promptQueries: CustomPromptObject[] = [
179
+ {
180
+ type: 'text',
181
+ name: 'slug',
182
+ message: 'What is the package slug?',
183
+ initial: defaultPackageSlug,
184
+ resolvedValue: options.target ? defaultPackageSlug : null,
185
+ },
186
+ {
187
+ type: 'text',
188
+ name: 'name',
189
+ message: 'What is the project name?',
190
+ initial: defaultProjectName,
191
+ },
192
+ {
193
+ type: 'text',
194
+ name: 'description',
195
+ message: 'How would you describe the module?',
196
+ },
197
+ {
198
+ type: 'text',
199
+ name: 'package',
200
+ message: 'What is the Android package name?',
201
+ initial: `expo.modules.${defaultPackageSlug.replace(/\W/g, '').toLowerCase()}`,
202
+ },
203
+ {
204
+ type: 'text',
205
+ name: 'author',
206
+ message: 'Who is the author?',
207
+ initial: (await npmWhoamiAsync(targetDir)) ?? '',
208
+ },
209
+ {
210
+ type: 'text',
211
+ name: 'license',
212
+ message: 'What is the license?',
213
+ initial: 'MIT',
214
+ },
215
+ {
216
+ type: 'text',
217
+ name: 'repo',
218
+ message: 'What is the repository URL?',
219
+ },
220
+ ];
221
+
222
+ const answers: Record<string, string> = {};
223
+ for (const query of promptQueries) {
224
+ const { name, resolvedValue } = query;
225
+ answers[name] = resolvedValue ?? options[name] ?? (await prompts(query))[name];
226
+ }
227
+
228
+ const { slug, name, description, package: projectPackage, author, license, repo } = answers;
229
+
230
+ return {
231
+ project: {
232
+ slug,
233
+ name,
234
+ version: '0.1.0',
235
+ description,
236
+ package: projectPackage,
237
+ },
238
+ author,
239
+ license,
240
+ repo,
241
+ };
242
+ }
243
+
244
+ const program = new Command();
245
+
246
+ program
247
+ .name(packageJson.name)
248
+ .version(packageJson.version)
249
+ .description(packageJson.description)
250
+ .arguments('[target_dir]')
251
+ .option(
252
+ '-s, --source <source_dir>',
253
+ 'Local path to the template. By default it downloads `expo-module-template` from NPM.'
254
+ )
255
+ .option('-n, --name <module_name>', 'Name of the native module.')
256
+ .option('-d, --description <description>', 'Description of the module.')
257
+ .option('-p, --package <package>', 'The Android package name.')
258
+ .option('-a, --author <author>', 'The author name.')
259
+ .option('-l, --license <license>', 'The license that the module is distributed with.')
260
+ .option('-r, --repo <repo_url>', 'The URL to the repository.')
261
+ .option('--with-readme', 'Whether to include README.md file.', false)
262
+ .option('--with-changelog', 'Whether to include CHANGELOG.md file.', false)
263
+ .action(main);
264
+
265
+ program.parse(process.argv);
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "@tsconfig/node12/tsconfig.json",
3
+ "include": ["./src"],
4
+ "exclude": ["**/__mocks__/*", "**/__tests__/*"],
5
+ "compilerOptions": {
6
+ "outDir": "./build",
7
+ "module": "commonjs",
8
+ "moduleResolution": "node",
9
+ "types": [],
10
+ "typeRoots": ["./ts-declarations", "node_modules/@types"],
11
+ "sourceMap": true,
12
+ "declaration": true,
13
+ "inlineSources": true,
14
+ "strictNullChecks": true,
15
+ "strictPropertyInitialization": true,
16
+ "strictFunctionTypes": true,
17
+ "skipLibCheck": true,
18
+ "noImplicitAny": false,
19
+ "noImplicitThis": true,
20
+ "noImplicitReturns": true,
21
+ "allowSyntheticDefaultImports": true
22
+ }
23
+ }