subpackage-update
Version:
Utilities for working with subpackage modules.
60 lines (59 loc) • 2.98 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function updateVersions() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
console.log('finding all package.json files...');
const currentPkg = JSON.parse(fs_1.default.readFileSync('package.json', 'utf-8'));
const workspaces = (_b = (_a = currentPkg.workspaces) === null || _a === void 0 ? void 0 : _a.packages) !== null && _b !== void 0 ? _b : currentPkg.workspaces;
if (!workspaces) {
console.log('No workspaces found.');
return;
}
const workspacePkgFiles = workspaces
.map((workspace) => {
const workspaceDir = workspace.replace('/*', '');
return fs_1.default
.readdirSync(workspaceDir)
.map((dir) => path_1.default.join(workspaceDir, dir, 'package.json'))
.filter((file) => fs_1.default.existsSync(file));
})
.flat();
const workspacePkgs = workspacePkgFiles.map((file) => JSON.parse(fs_1.default.readFileSync(file, 'utf-8')));
workspacePkgs.forEach((pkg) => {
console.log(`Package: ${pkg.name}, Version ${pkg.version}`);
});
workspacePkgFiles.forEach((file) => {
// console.log(`ok dealing with ${file}`);
const fileContent = fs_1.default.readFileSync(file, 'utf-8');
let newFileContent = fs_1.default.readFileSync(file, 'utf-8');
workspacePkgs.forEach((pkg) => {
if (pkg.name && pkg.version) {
newFileContent = newFileContent.replace(new RegExp(`"${pkg.name}": "\\^[0-9]+\\.[0-9]+\\.[0-9]+"`, 'g'), `"${pkg.name}": "^${pkg.version}"`);
}
});
if (fileContent !== newFileContent) {
console.log(`Updating: ${file}`);
fs_1.default.writeFileSync(file, newFileContent);
}
});
});
}
updateVersions().then(() => {
console.log('Done!');
});
;