forked from nodejs-mobile/nodejs-mobile-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-node-structure.js
More file actions
123 lines (106 loc) · 3.38 KB
/
create-node-structure.js
File metadata and controls
123 lines (106 loc) · 3.38 KB
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
//Adapted from the www.npmjs.com/package/install-files project
const path = require('path');
const fs = require('fs');
const ncp = require('ncp');
const mkdirp = require('mkdirp');
function hostPackageDir(file) {
var pathComponents = file.split(path.sep);
var modulesDirIndex = pathComponents.lastIndexOf('node_modules');
if (modulesDirIndex < 1) return undefined;
return pathComponents.slice(0, modulesDirIndex).join(path.sep);
}
function getModulePackageName(target) {
var packageName;
try {
packageName = JSON.parse(
fs.readFileSync(path.join(target, 'package.json'), 'utf8'),
).name;
} catch (e) {
packageName = path.basename(target);
}
return packageName;
}
function installFiles(done) {
var lifecycleEvent = process.env.npm_lifecycle_event;
var packageIsInstalling =
lifecycleEvent === 'install' || lifecycleEvent === 'postinstall';
if (!packageIsInstalling) {
var error1 = new Error(
"This module is meant to be invoked from a package's 'install' or 'postinstall' script.",
);
process.nextTick(() => done(error1));
return;
}
var scriptPath = __filename;
// The path to the package running the 'install' or 'postinstall' script.
var fileInstallingPackagePath = hostPackageDir(scriptPath);
// The target package responsible for the 'install' or 'postinstall' event
var installTargetPackageName = process.env.npm_package_name;
var source, target;
source = path.join(
fileInstallingPackagePath,
'node_modules',
installTargetPackageName,
'install',
'resources',
'nodejs-assets',
);
target = fileInstallingPackagePath;
if (!target) {
var error2 = new Error(
'Could not determine the install destination directory.',
);
process.nextTick(() => done(error2));
return;
}
// If install destination is the current module, we can silently skip
if (installTargetPackageName == getModulePackageName(target)) {
process.nextTick(() => done());
return;
}
target = path.join(target, 'nodejs-assets');
//make sure target path exists
mkdirp(target, function (err) {
if (err) process.nextTick(() => done(err));
return;
});
// Overwrite existing files with colliding filenames. Copy node-assets only.
const options = {
clobber: true,
};
ncp(source, target, options, done);
if (process.platform === 'darwin') {
// Adds a helper scripts to run "npm rebuild" and "node" with the current PATH.
// This workaround is needed for Android Studio on macOS when it is not started
// from the command line, as npm and node probably won't be in the PATH at build time.
var helperMacOSBuildScriptPath = path.join(
target,
'build-native-modules-MacOS-helper-script-npm.sh',
);
fs.writeFileSync(
helperMacOSBuildScriptPath,
`#!/bin/bash
# Helper script for Gradle to call npm on macOS in case it is not found
export PATH=$PATH:${process.env.PATH}
npm $@
`,
{mode: 0o755},
);
helperMacOSBuildScriptPath = path.join(
target,
'build-native-modules-MacOS-helper-script-node.sh',
);
fs.writeFileSync(
helperMacOSBuildScriptPath,
`#!/bin/bash
# Helper script for Gradle to call node on macOS in case it is not found
export PATH=$PATH:${process.env.PATH}
node $@
`,
{mode: 0o755},
);
}
}
installFiles(function (err) {
if (err) console.error(err);
});