@socketsecurity/lib
Version:
Core utilities and infrastructure for Socket.dev security tools
359 lines (358 loc) • 11.8 kB
JavaScript
;
/* Socket Lib - Built with esbuild */
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var editable_exports = {};
__export(editable_exports, {
getEditablePackageJsonClass: () => getEditablePackageJsonClass,
pkgJsonToEditable: () => pkgJsonToEditable,
toEditablePackageJson: () => toEditablePackageJson,
toEditablePackageJsonSync: () => toEditablePackageJsonSync
});
module.exports = __toCommonJS(editable_exports);
var import_path = require("../path");
var import_normalize = require("./normalize");
var import_paths = require("./paths");
const identSymbol = Symbol.for("indent");
const newlineSymbol = Symbol.for("newline");
let _EditablePackageJsonClass;
let _fs;
// @__NO_SIDE_EFFECTS__
function getFs() {
if (_fs === void 0) {
_fs = require("node:fs");
}
return _fs;
}
let _path;
// @__NO_SIDE_EFFECTS__
function getPath() {
if (_path === void 0) {
_path = require("node:path");
}
return _path;
}
let _util;
// @__NO_SIDE_EFFECTS__
function getUtil() {
if (_util === void 0) {
_util = require("node:util");
}
return _util;
}
// @__NO_SIDE_EFFECTS__
function getEditablePackageJsonClass() {
if (_EditablePackageJsonClass === void 0) {
const EditablePackageJsonBase = require("../external/@npmcli/package-json");
const { parse, read } = require("../external/@npmcli/package-json/lib/read-package");
const { packageSort } = require("../external/@npmcli/package-json/lib/sort");
_EditablePackageJsonClass = class EditablePackageJson extends EditablePackageJsonBase {
static fixSteps = EditablePackageJsonBase.fixSteps;
static normalizeSteps = EditablePackageJsonBase.normalizeSteps;
static prepareSteps = EditablePackageJsonBase.prepareSteps;
_canSave = true;
_path = void 0;
_readFileContent = "";
_readFileJson = void 0;
get content() {
return super.content;
}
get filename() {
const path = this._path;
if (!path) {
return "";
}
if (path.endsWith("package.json")) {
return path;
}
const nodePath = /* @__PURE__ */ getPath();
return nodePath.join(path, "package.json");
}
static async create(path, opts = {}) {
const p = new _EditablePackageJsonClass();
await p.create(path);
return opts.data ? p.update(opts.data) : p;
}
static async fix(path, opts) {
const p = new _EditablePackageJsonClass();
await p.load(path, true);
return await p.fix(opts);
}
static async load(path, opts = {}) {
const p = new _EditablePackageJsonClass();
if (!opts.create) {
return await p.load(path);
}
try {
return await p.load(path);
} catch (err) {
if (!err.message.startsWith("Could not read package.json")) {
throw err;
}
return p.create(path);
}
}
static async normalize(path, opts) {
const p = new _EditablePackageJsonClass();
await p.load(path);
return await p.normalize(opts);
}
static async prepare(path, opts) {
const p = new _EditablePackageJsonClass();
await p.load(path, true);
return await p.prepare(opts);
}
create(path) {
super.create(path);
this._path = path;
return this;
}
async fix(opts = {}) {
await super.fix(opts);
return this;
}
fromContent(data) {
super.fromContent(data);
this._canSave = false;
return this;
}
fromJSON(data) {
super.fromJSON(data);
return this;
}
async load(path, create) {
this._path = path;
const { promises: fsPromises } = /* @__PURE__ */ getFs();
let parseErr;
try {
this._readFileContent = await read(this.filename);
} catch (err) {
if (!create) {
throw err;
}
parseErr = err;
}
if (parseErr) {
const nodePath = /* @__PURE__ */ getPath();
const indexFile = nodePath.resolve(this.path || "", "index.js");
let indexFileContent;
try {
indexFileContent = await fsPromises.readFile(indexFile, "utf8");
} catch {
throw parseErr;
}
try {
this.fromContent(indexFileContent);
} catch {
throw parseErr;
}
this._canSave = false;
return this;
}
this.fromJSON(this._readFileContent);
this._readFileJson = parse(this._readFileContent);
return this;
}
async normalize(opts = {}) {
await super.normalize(opts);
return this;
}
get path() {
return this._path;
}
async prepare(opts = {}) {
await super.prepare(opts);
return this;
}
async save(options) {
if (!this._canSave || this.content === void 0) {
throw new Error("No package.json to save to");
}
const { ignoreWhitespace = false, sort = false } = {
__proto__: null,
...options
};
const {
[identSymbol]: indent,
[newlineSymbol]: newline,
...rest
} = this.content;
const content = sort ? packageSort(rest) : rest;
const {
[identSymbol]: _indent,
[newlineSymbol]: _newline,
...origContent
} = this._readFileJson || {};
if (ignoreWhitespace && (/* @__PURE__ */ getUtil()).isDeepStrictEqual(content, origContent)) {
return false;
}
const format = indent === void 0 || indent === null ? " " : indent;
const eol = newline === void 0 || newline === null ? "\n" : newline;
const fileContent = `${JSON.stringify(
content,
void 0,
format
)}
`.replace(/\n/g, eol);
if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) {
return false;
}
const { promises: fsPromises } = /* @__PURE__ */ getFs();
await fsPromises.writeFile(this.filename, fileContent);
this._readFileContent = fileContent;
this._readFileJson = parse(fileContent);
return true;
}
saveSync(options) {
if (!this._canSave || this.content === void 0) {
throw new Error("No package.json to save to");
}
const { ignoreWhitespace = false, sort = false } = {
__proto__: null,
...options
};
const {
[Symbol.for("indent")]: indent,
[Symbol.for("newline")]: newline,
...rest
} = this.content;
const content = sort ? packageSort(rest) : rest;
if (ignoreWhitespace && (/* @__PURE__ */ getUtil()).isDeepStrictEqual(content, this._readFileJson)) {
return false;
}
const format = indent === void 0 || indent === null ? " " : indent;
const eol = newline === void 0 || newline === null ? "\n" : newline;
const fileContent = `${JSON.stringify(
content,
void 0,
format
)}
`.replace(/\n/g, eol);
if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) {
return false;
}
const fs = /* @__PURE__ */ getFs();
fs.writeFileSync(this.filename, fileContent);
this._readFileContent = fileContent;
this._readFileJson = parse(fileContent);
return true;
}
update(content) {
super.update(content);
return this;
}
willSave(options) {
const { ignoreWhitespace = false, sort = false } = {
__proto__: null,
...options
};
if (!this._canSave || this.content === void 0) {
return false;
}
const {
[Symbol.for("indent")]: indent,
[Symbol.for("newline")]: newline,
...rest
} = this.content;
const content = sort ? packageSort(rest) : rest;
if (ignoreWhitespace && (/* @__PURE__ */ getUtil()).isDeepStrictEqual(content, this._readFileJson)) {
return false;
}
const format = indent === void 0 || indent === null ? " " : indent;
const eol = newline === void 0 || newline === null ? "\n" : newline;
const fileContent = `${JSON.stringify(
content,
void 0,
format
)}
`.replace(/\n/g, eol);
if (!ignoreWhitespace && fileContent.trim() === this._readFileContent.trim()) {
return false;
}
return true;
}
};
}
return _EditablePackageJsonClass;
}
// @__NO_SIDE_EFFECTS__
function pkgJsonToEditable(pkgJson, options) {
const { normalize, ...normalizeOptions } = {
__proto__: null,
...options
};
const EditablePackageJson = /* @__PURE__ */ getEditablePackageJsonClass();
return new EditablePackageJson().fromContent(
normalize ? (0, import_normalize.normalizePackageJson)(pkgJson, normalizeOptions) : pkgJson
);
}
// @__NO_SIDE_EFFECTS__
async function toEditablePackageJson(pkgJson, options) {
const { path: filepath, ...pkgJsonToEditableOptions } = {
__proto__: null,
...options
};
const { normalize, ...normalizeOptions } = pkgJsonToEditableOptions;
if (typeof filepath !== "string") {
return /* @__PURE__ */ pkgJsonToEditable(pkgJson, pkgJsonToEditableOptions);
}
const EditablePackageJson = /* @__PURE__ */ getEditablePackageJsonClass();
const pkgJsonPath = (0, import_paths.resolvePackageJsonDirname)(filepath);
return (await EditablePackageJson.load(pkgJsonPath, { create: true })).fromJSON(
`${JSON.stringify(
normalize ? (0, import_normalize.normalizePackageJson)(pkgJson, {
...(0, import_path.isNodeModules)(pkgJsonPath) ? {} : { preserve: ["repository"] },
...normalizeOptions
}) : pkgJson,
null,
2
)}
`
);
}
// @__NO_SIDE_EFFECTS__
function toEditablePackageJsonSync(pkgJson, options) {
const { path: filepath, ...pkgJsonToEditableOptions } = {
__proto__: null,
...options
};
const { normalize, ...normalizeOptions } = pkgJsonToEditableOptions;
if (typeof filepath !== "string") {
return /* @__PURE__ */ pkgJsonToEditable(pkgJson, pkgJsonToEditableOptions);
}
const EditablePackageJson = /* @__PURE__ */ getEditablePackageJsonClass();
const pkgJsonPath = (0, import_paths.resolvePackageJsonDirname)(filepath);
return new EditablePackageJson().create(pkgJsonPath).fromJSON(
`${JSON.stringify(
normalize ? (0, import_normalize.normalizePackageJson)(pkgJson, {
...(0, import_path.isNodeModules)(pkgJsonPath) ? {} : { preserve: ["repository"] },
...normalizeOptions
}) : pkgJson,
null,
2
)}
`
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getEditablePackageJsonClass,
pkgJsonToEditable,
toEditablePackageJson,
toEditablePackageJsonSync
});