UNPKG

4.48 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const path_1 = __importDefault(require("path"));
7const fs_extra_1 = __importDefault(require("fs-extra"));
8const glob_1 = require("glob");
9function getPackage(config) {
10 if (config.android && config.android.package) {
11 return config.android.package;
12 }
13 return null;
14}
15exports.getPackage = getPackage;
16function getPackageRoot(projectRoot) {
17 return path_1.default.join(projectRoot, 'android', 'app', 'src', 'main', 'java');
18}
19function getCurrentPackageName(projectRoot) {
20 let packageRoot = getPackageRoot(projectRoot);
21 let mainApplicationPath = glob_1.sync(path_1.default.join(packageRoot, '**', 'MainApplication.java'))[0];
22 let packagePath = path_1.default.dirname(mainApplicationPath);
23 let packagePathParts = packagePath
24 .replace(packageRoot, '')
25 .split(path_1.default.sep)
26 .filter(Boolean);
27 return packagePathParts.join('.');
28}
29// NOTE(brentvatne): this assumes that our MainApplication.java file is in the root of the package
30// this makes sense for standard react-native projects but may not apply in customized projects, so if
31// we want this to be runnable in any app we need to handle other possibilities
32function renamePackageOnDisk(config, projectRoot) {
33 const newPackageName = getPackage(config);
34 if (newPackageName === null) {
35 return;
36 }
37 const currentPackageName = getCurrentPackageName(projectRoot);
38 if (currentPackageName === newPackageName) {
39 return;
40 }
41 // Set up our paths
42 let packageRoot = getPackageRoot(projectRoot);
43 let currentPackagePath = path_1.default.join(packageRoot, ...currentPackageName.split('.'));
44 let newPackagePath = path_1.default.join(packageRoot, ...newPackageName.split('.'));
45 // Create the new directory
46 fs_extra_1.default.mkdirpSync(newPackagePath);
47 // Move everything from the old directory over
48 glob_1.sync(path_1.default.join(currentPackagePath, '**', '*')).forEach(filepath => {
49 let relativePath = filepath.replace(currentPackagePath, '');
50 if (fs_extra_1.default.lstatSync(filepath).isFile()) {
51 fs_extra_1.default.moveSync(filepath, path_1.default.join(newPackagePath, relativePath));
52 }
53 else {
54 fs_extra_1.default.mkdirpSync(filepath);
55 }
56 });
57 // Remove the old directory recursively from com/old/package to com/old and com,
58 // as long as the directories are empty
59 let oldPathParts = currentPackageName.split('.');
60 while (oldPathParts.length) {
61 let pathToCheck = path_1.default.join(packageRoot, ...oldPathParts);
62 try {
63 let files = fs_extra_1.default.readdirSync(pathToCheck);
64 if (files.length === 0) {
65 fs_extra_1.default.rmdirSync(pathToCheck);
66 }
67 }
68 catch (_) {
69 }
70 finally {
71 oldPathParts.pop();
72 }
73 }
74 const filesToUpdate = [
75 ...glob_1.sync(path_1.default.join(newPackagePath, '**', '*')),
76 path_1.default.join(projectRoot, 'android', 'app', 'BUCK'),
77 ];
78 // Replace all occurrences of the path in the project
79 filesToUpdate.forEach((filepath) => {
80 try {
81 if (fs_extra_1.default.lstatSync(filepath).isFile()) {
82 let contents = fs_extra_1.default.readFileSync(filepath).toString();
83 contents = contents.replace(new RegExp(currentPackageName, 'g'), newPackageName);
84 fs_extra_1.default.writeFileSync(filepath, contents);
85 }
86 }
87 catch (_) { }
88 });
89}
90exports.renamePackageOnDisk = renamePackageOnDisk;
91function setPackageInBuildGradle(config, buildGradle) {
92 let packageName = getPackage(config);
93 if (packageName === null) {
94 return buildGradle;
95 }
96 let pattern = new RegExp(`applicationId ['"].*['"]`);
97 return buildGradle.replace(pattern, `applicationId '${packageName}'`);
98}
99exports.setPackageInBuildGradle = setPackageInBuildGradle;
100async function setPackageInAndroidManifest(config, manifestDocument) {
101 let packageName = getPackage(config);
102 manifestDocument['manifest']['$']['package'] = packageName;
103 return manifestDocument;
104}
105exports.setPackageInAndroidManifest = setPackageInAndroidManifest;
106//# sourceMappingURL=Package.js.map
\No newline at end of file