extensions.js 12 KB

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