extensions.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*!
  2. * node-sass: lib/extensions.js
  3. */
  4. var eol = require('os').EOL,
  5. fs = require('fs'),
  6. pkg = require('../package.json'),
  7. mkdir = require('mkdirp'),
  8. path = require('path'),
  9. defaultBinaryDir = path.join(__dirname, '..', 'vendor'),
  10. trueCasePathSync = require('true-case-path');
  11. /**
  12. * Get the human readable name of the Platform that is running
  13. *
  14. * @param {string} platform - An OS platform to match, or null to fallback to
  15. * the current process platform
  16. * @return {Object} The name of the platform if matched, false otherwise
  17. *
  18. * @api public
  19. */
  20. function getHumanPlatform(platform) {
  21. switch (platform || process.platform) {
  22. case 'darwin': return 'OS X';
  23. case 'freebsd': return 'FreeBSD';
  24. case 'linux': return 'Linux';
  25. case 'linux_musl': return 'Linux/musl';
  26. case 'win32': return 'Windows';
  27. default: return false;
  28. }
  29. }
  30. /**
  31. * Provides a more readable version of the architecture
  32. *
  33. * @param {string} arch - An instruction architecture name to match, or null to
  34. * lookup the current process architecture
  35. * @return {Object} The value of the process architecture, or false if unknown
  36. *
  37. * @api public
  38. */
  39. function getHumanArchitecture(arch) {
  40. switch (arch || process.arch) {
  41. case 'ia32': return '32-bit';
  42. case 'x86': return '32-bit';
  43. case 'x64': return '64-bit';
  44. default: return false;
  45. }
  46. }
  47. /**
  48. * Get the friendly name of the Node environment being run
  49. *
  50. * @param {Object} abi - A Node Application Binary Interface value, or null to
  51. * fallback to the current Node ABI
  52. * @return {Object} Returns a string name of the Node environment or false if
  53. * unmatched
  54. *
  55. * @api public
  56. */
  57. function getHumanNodeVersion(abi) {
  58. switch (parseInt(abi || process.versions.modules, 10)) {
  59. case 11: return 'Node 0.10.x';
  60. case 14: return 'Node 0.12.x';
  61. case 42: return 'io.js 1.x';
  62. case 43: return 'io.js 1.1.x';
  63. case 44: return 'io.js 2.x';
  64. case 45: return 'io.js 3.x';
  65. case 46: return 'Node.js 4.x';
  66. case 47: return 'Node.js 5.x';
  67. case 48: return 'Node.js 6.x';
  68. case 49: return 'Electron 1.3.x';
  69. case 50: return 'Electron 1.4.x';
  70. case 51: return 'Node.js 7.x';
  71. case 53: return 'Electron 1.6.x';
  72. case 57: return 'Node.js 8.x';
  73. case 59: return 'Node.js 9.x';
  74. case 63: return 'Node.js 10.x';
  75. default: return false;
  76. }
  77. }
  78. /**
  79. * Get a human readable description of where node-sass is running to support
  80. * user error reporting when something goes wrong
  81. *
  82. * @param {string} env - The name of the native bindings that is to be parsed
  83. * @return {string} A description of what os, architecture, and Node version
  84. * that is being run
  85. *
  86. * @api public
  87. */
  88. function getHumanEnvironment(env) {
  89. var binding = env.replace(/_binding\.node$/, ''),
  90. parts = binding.split('-'),
  91. platform = getHumanPlatform(parts[0]),
  92. arch = getHumanArchitecture(parts[1]),
  93. runtime = getHumanNodeVersion(parts[2]);
  94. if (parts.length !== 3) {
  95. return 'Unknown environment (' + binding + ')';
  96. }
  97. if (!platform) {
  98. platform = 'Unsupported platform (' + parts[0] + ')';
  99. }
  100. if (!arch) {
  101. arch = 'Unsupported architecture (' + parts[1] + ')';
  102. }
  103. if (!runtime) {
  104. runtime = 'Unsupported runtime (' + parts[2] + ')';
  105. }
  106. return [
  107. platform, arch, 'with', runtime,
  108. ].join(' ');
  109. }
  110. /**
  111. * Get the value of the binaries under the default path
  112. *
  113. * @return {Array} The currently installed node-sass bindings
  114. *
  115. * @api public
  116. */
  117. function getInstalledBinaries() {
  118. return fs.readdirSync(getBinaryDir());
  119. }
  120. /**
  121. * Check that an environment matches the whitelisted values or the current
  122. * environment if no parameters are passed
  123. *
  124. * @param {string} platform - The name of the OS platform(darwin, win32, etc...)
  125. * @param {string} arch - The instruction set architecture of the Node environment
  126. * @param {string} abi - The Node Application Binary Interface
  127. * @return {Boolean} True, if node-sass supports the current platform, false otherwise
  128. *
  129. * @api public
  130. */
  131. function isSupportedEnvironment(platform, arch, abi) {
  132. return (
  133. false !== getHumanPlatform(platform) &&
  134. false !== getHumanArchitecture(arch) &&
  135. false !== getHumanNodeVersion(abi)
  136. );
  137. }
  138. /**
  139. * Get the value of a CLI argument
  140. *
  141. * @param {String} name
  142. * @param {Array} args
  143. * @api private
  144. */
  145. function getArgument(name, args) {
  146. var flags = args || process.argv.slice(2),
  147. index = flags.lastIndexOf(name);
  148. if (index === -1 || index + 1 >= flags.length) {
  149. return null;
  150. }
  151. return flags[index + 1];
  152. }
  153. /**
  154. * Get binary name.
  155. * If environment variable SASS_BINARY_NAME,
  156. * .npmrc variable sass_binary_name or
  157. * process argument --binary-name is provided,
  158. * return it as is, otherwise make default binary
  159. * name: {platform}-{arch}-{v8 version}.node
  160. *
  161. * @api public
  162. */
  163. function getBinaryName() {
  164. var binaryName,
  165. variant,
  166. platform = process.platform;
  167. if (getArgument('--sass-binary-name')) {
  168. binaryName = getArgument('--sass-binary-name');
  169. } else if (process.env.SASS_BINARY_NAME) {
  170. binaryName = process.env.SASS_BINARY_NAME;
  171. } else if (process.env.npm_config_sass_binary_name) {
  172. binaryName = process.env.npm_config_sass_binary_name;
  173. } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryName) {
  174. binaryName = pkg.nodeSassConfig.binaryName;
  175. } else {
  176. variant = getPlatformVariant();
  177. if (variant) {
  178. platform += '_' + variant;
  179. }
  180. binaryName = [
  181. platform, '-',
  182. process.arch, '-',
  183. process.versions.modules
  184. ].join('');
  185. }
  186. return [binaryName, 'binding.node'].join('_');
  187. }
  188. /**
  189. * Determine the URL to fetch binary file from.
  190. * By default fetch from the node-sass distribution
  191. * site on GitHub.
  192. *
  193. * The default URL can be overriden using
  194. * the environment variable SASS_BINARY_SITE,
  195. * .npmrc variable sass_binary_site or
  196. * or a command line option --sass-binary-site:
  197. *
  198. * node scripts/install.js --sass-binary-site http://example.com/
  199. *
  200. * The URL should to the mirror of the repository
  201. * laid out as follows:
  202. *
  203. * SASS_BINARY_SITE/
  204. *
  205. * v3.0.0
  206. * v3.0.0/freebsd-x64-14_binding.node
  207. * ....
  208. * v3.0.0
  209. * v3.0.0/freebsd-ia32-11_binding.node
  210. * v3.0.0/freebsd-x64-42_binding.node
  211. * ... etc. for all supported versions and platforms
  212. *
  213. * @api public
  214. */
  215. function getBinaryUrl() {
  216. var site = getArgument('--sass-binary-site') ||
  217. process.env.SASS_BINARY_SITE ||
  218. process.env.npm_config_sass_binary_site ||
  219. (pkg.nodeSassConfig && pkg.nodeSassConfig.binarySite) ||
  220. 'https://github.com/sass/node-sass/releases/download';
  221. return [site, 'v' + pkg.version, getBinaryName()].join('/');
  222. }
  223. /**
  224. * Get binary dir.
  225. * If environment variable SASS_BINARY_DIR,
  226. * .npmrc variable sass_binary_dir or
  227. * process argument --sass-binary-dir is provided,
  228. * select it by appending binary name, otherwise
  229. * use default binary dir.
  230. * Once the primary selection is made, check if
  231. * callers wants to throw if file not exists before
  232. * returning.
  233. *
  234. * @api public
  235. */
  236. function getBinaryDir() {
  237. var binaryDir;
  238. if (getArgument('--sass-binary-dir')) {
  239. binaryDir = getArgument('--sass-binary-dir');
  240. } else if (process.env.SASS_BINARY_DIR) {
  241. binaryDir = process.env.SASS_BINARY_DIR;
  242. } else if (process.env.npm_config_sass_binary_dir) {
  243. binaryDir = process.env.npm_config_sass_binary_dir;
  244. } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryDir) {
  245. binaryDir = pkg.nodeSassConfig.binaryDir;
  246. } else {
  247. binaryDir = defaultBinaryDir;
  248. }
  249. return binaryDir;
  250. }
  251. /**
  252. * Get binary path.
  253. * If environment variable SASS_BINARY_PATH,
  254. * .npmrc variable sass_binary_path or
  255. * process argument --sass-binary-path is provided,
  256. * select it by appending binary name, otherwise
  257. * make default binary path using binary name.
  258. * Once the primary selection is made, check if
  259. * callers wants to throw if file not exists before
  260. * returning.
  261. *
  262. * @api public
  263. */
  264. function getBinaryPath() {
  265. var binaryPath;
  266. if (getArgument('--sass-binary-path')) {
  267. binaryPath = getArgument('--sass-binary-path');
  268. } else if (process.env.SASS_BINARY_PATH) {
  269. binaryPath = process.env.SASS_BINARY_PATH;
  270. } else if (process.env.npm_config_sass_binary_path) {
  271. binaryPath = process.env.npm_config_sass_binary_path;
  272. } else if (pkg.nodeSassConfig && pkg.nodeSassConfig.binaryPath) {
  273. binaryPath = pkg.nodeSassConfig.binaryPath;
  274. } else {
  275. binaryPath = path.join(getBinaryDir(), getBinaryName().replace(/_(?=binding\.node)/, '/'));
  276. }
  277. if (process.versions.modules < 46) {
  278. return binaryPath;
  279. }
  280. try {
  281. return trueCasePathSync(binaryPath) || binaryPath;
  282. } catch (e) {
  283. return binaryPath;
  284. }
  285. }
  286. /**
  287. * An array of paths suitable for use as a local disk cache of the binding.
  288. *
  289. * @return {[]String} an array of paths
  290. * @api public
  291. */
  292. function getCachePathCandidates() {
  293. return [
  294. process.env.npm_config_sass_binary_cache,
  295. process.env.npm_config_cache,
  296. ].filter(function(_) { return _; });
  297. }
  298. /**
  299. * The most suitable location for caching the binding on disk.
  300. *
  301. * Given the candidates directories provided by `getCachePathCandidates()` this
  302. * returns the first writable directory. By treating the candidate directories
  303. * as a prioritised list this method is deterministic, assuming no change to the
  304. * local environment.
  305. *
  306. * @return {String} directory to cache binding
  307. * @api public
  308. */
  309. function getBinaryCachePath() {
  310. var i,
  311. cachePath,
  312. cachePathCandidates = getCachePathCandidates();
  313. for (i = 0; i < cachePathCandidates.length; i++) {
  314. cachePath = path.join(cachePathCandidates[i], pkg.name, pkg.version);
  315. try {
  316. mkdir.sync(cachePath);
  317. return cachePath;
  318. } catch (e) {
  319. // Directory is not writable, try another
  320. }
  321. }
  322. return '';
  323. }
  324. /**
  325. * The cached binding
  326. *
  327. * Check the candidates directories provided by `getCachePathCandidates()` for
  328. * the binding file, if it exists. By treating the candidate directories
  329. * as a prioritised list this method is deterministic, assuming no change to the
  330. * local environment.
  331. *
  332. * @return {String} path to cached binary
  333. * @api public
  334. */
  335. function getCachedBinary() {
  336. var i,
  337. cachePath,
  338. cacheBinary,
  339. cachePathCandidates = getCachePathCandidates(),
  340. binaryName = getBinaryName();
  341. for (i = 0; i < cachePathCandidates.length; i++) {
  342. cachePath = path.join(cachePathCandidates[i], pkg.name, pkg.version);
  343. cacheBinary = path.join(cachePath, binaryName);
  344. if (fs.existsSync(cacheBinary)) {
  345. return cacheBinary;
  346. }
  347. }
  348. return '';
  349. }
  350. /**
  351. * Does the supplied binary path exist
  352. *
  353. * @param {String} binaryPath
  354. * @api public
  355. */
  356. function hasBinary(binaryPath) {
  357. return fs.existsSync(binaryPath);
  358. }
  359. /**
  360. * Get Sass version information
  361. *
  362. * @api public
  363. */
  364. function getVersionInfo(binding) {
  365. return [
  366. ['node-sass', pkg.version, '(Wrapper)', '[JavaScript]'].join('\t'),
  367. ['libsass ', binding.libsassVersion(), '(Sass Compiler)', '[C/C++]'].join('\t'),
  368. ].join(eol);
  369. }
  370. /**
  371. * Gets the platform variant, currently either an empty string or 'musl' for Linux/musl platforms.
  372. *
  373. * @api public
  374. */
  375. function getPlatformVariant() {
  376. var contents = '';
  377. if (process.platform !== 'linux') {
  378. return '';
  379. }
  380. try {
  381. contents = fs.readFileSync(process.execPath);
  382. // Buffer.indexOf was added in v1.5.0 so cast to string for old node
  383. // Delay contents.toStrings because it's expensive
  384. if (!contents.indexOf) {
  385. contents = contents.toString();
  386. }
  387. if (contents.indexOf('libc.musl-x86_64.so.1') !== -1) {
  388. return 'musl';
  389. }
  390. } catch (err) { } // eslint-disable-line no-empty
  391. return '';
  392. }
  393. module.exports.hasBinary = hasBinary;
  394. module.exports.getBinaryUrl = getBinaryUrl;
  395. module.exports.getBinaryName = getBinaryName;
  396. module.exports.getBinaryDir = getBinaryDir;
  397. module.exports.getBinaryPath = getBinaryPath;
  398. module.exports.getBinaryCachePath = getBinaryCachePath;
  399. module.exports.getCachedBinary = getCachedBinary;
  400. module.exports.getCachePathCandidates = getCachePathCandidates;
  401. module.exports.getVersionInfo = getVersionInfo;
  402. module.exports.getHumanEnvironment = getHumanEnvironment;
  403. module.exports.getInstalledBinaries = getInstalledBinaries;
  404. module.exports.isSupportedEnvironment = isSupportedEnvironment;