mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-04 03:43:34 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			139 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			139 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
'use strict';
 | 
						|
 | 
						|
/**
 | 
						|
 * Module dependencies
 | 
						|
 */
 | 
						|
 | 
						|
// Node.js core.
 | 
						|
const fs = require('fs');
 | 
						|
 | 
						|
// Public node modules.
 | 
						|
const _ = require('lodash');
 | 
						|
 | 
						|
/**
 | 
						|
 * `parseJSONFile()`
 | 
						|
 *
 | 
						|
 * Read a json file at the specified path.
 | 
						|
 * If an error occurs, call cb(err), and dont throw!
 | 
						|
 *
 | 
						|
 * @api private
 | 
						|
 */
 | 
						|
 | 
						|
exports.parseJSONFile = function (path, cb) {
 | 
						|
  if (!cb) {
 | 
						|
    throw new Error('Callback required!');
 | 
						|
  }
 | 
						|
 | 
						|
  if (cb === 'sync') {
 | 
						|
    let jsonString;
 | 
						|
 | 
						|
    try {
 | 
						|
      jsonString = fs.readFileSync(path, 'utf-8');
 | 
						|
    } catch (e) {
 | 
						|
      return false;
 | 
						|
    }
 | 
						|
 | 
						|
    return andThen(jsonString);
 | 
						|
  }
 | 
						|
 | 
						|
  fs.readFile(path, 'utf-8', function (err, file) {
 | 
						|
    if (err) {
 | 
						|
      return cb(err);
 | 
						|
    }
 | 
						|
 | 
						|
    andThen(file);
 | 
						|
  });
 | 
						|
 | 
						|
  // Attempt to parse JSON, then return.
 | 
						|
  function andThen(json) {
 | 
						|
    let err;
 | 
						|
 | 
						|
    try {
 | 
						|
      json = JSON.parse(json);
 | 
						|
    } catch (e) {
 | 
						|
      err = e;
 | 
						|
      json = false;
 | 
						|
    }
 | 
						|
 | 
						|
    // Parse failed.
 | 
						|
    if (err) {
 | 
						|
      if (cb === 'sync') {
 | 
						|
        return false;
 | 
						|
      }
 | 
						|
      return cb(err);
 | 
						|
    }
 | 
						|
 | 
						|
    // Success.
 | 
						|
    if (cb === 'sync') {
 | 
						|
      return json;
 | 
						|
    }
 | 
						|
 | 
						|
    return cb(null, json);
 | 
						|
  }
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * `getJSONFileSync()`
 | 
						|
 *
 | 
						|
 * Synchronous version of `getJSONFile()`.
 | 
						|
 * Returns false if json file cannot be read or parsed.
 | 
						|
 *
 | 
						|
 * @api private
 | 
						|
 */
 | 
						|
 | 
						|
exports.parseJSONFileSync = function (path) {
 | 
						|
  return exports.parseJSONFile(path, 'sync');
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * `getPackage()`
 | 
						|
 *
 | 
						|
 * Read `package.json` file in the directory at the specified
 | 
						|
 * path. If an error occurs, call `cb(err)`, and dont throw!
 | 
						|
 *
 | 
						|
 * @api private
 | 
						|
 */
 | 
						|
 | 
						|
exports.getPackage = function (path, cb) {
 | 
						|
  path = _.trimRight(path, '/');
 | 
						|
  path += '/package.json';
 | 
						|
 | 
						|
  exports.parseJSONFile(path, function (err, json) {
 | 
						|
    if (err) {
 | 
						|
      return cb(err);
 | 
						|
    }
 | 
						|
 | 
						|
    // Success: ensure dependencies are at least an empty object
 | 
						|
    json.dependencies = json.dependencies || {};
 | 
						|
    if (cb === 'sync') {
 | 
						|
      return json;
 | 
						|
    }
 | 
						|
 | 
						|
    return cb(null, json);
 | 
						|
  });
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * `getPackageSync()`
 | 
						|
 *
 | 
						|
 * Synchronous version of `getPackage()`
 | 
						|
 * Returns `false` if `package.json` cannot be read or parsed.
 | 
						|
 *
 | 
						|
 * @api private
 | 
						|
 */
 | 
						|
 | 
						|
exports.getPackageSync = function (path) {
 | 
						|
  path = _.trimRight(path, '/');
 | 
						|
  path += '/package.json';
 | 
						|
 | 
						|
  // Success: ensure dependencies are at least an empty object.
 | 
						|
  const json = exports.parseJSONFileSync(path, 'sync');
 | 
						|
 | 
						|
  if (!json) {
 | 
						|
    return json;
 | 
						|
  }
 | 
						|
 | 
						|
  json.dependencies = json.dependencies || {};
 | 
						|
  return json;
 | 
						|
};
 |