Browse Source

Publish public packages with access public (#5310)

* Add access public to template

* Add access public to addsibling

* Create json file writer and use it

* formatting
Steven Silvester 6 years ago
parent
commit
7179f9e135

+ 1 - 0
buildutils/package.json

@@ -37,6 +37,7 @@
     "watch": "tsc -w --listEmittedFiles"
   },
   "dependencies": {
+    "@phosphor/coreutils": "^1.3.0",
     "child_process": "~1.0.2",
     "fs-extra": "~4.0.2",
     "glob": "~7.1.2",

+ 8 - 2
buildutils/src/add-sibling.ts

@@ -51,7 +51,13 @@ if (fs.existsSync(path.join(packagePath, 'node_modules'))) {
 }
 
 // Get the package.json of the extension.
-let data = utils.readJSONFile(path.join(packagePath, 'package.json'));
+let pkgJSONPath = path.join(packagePath, 'package.json');
+let data = utils.readJSONFile(pkgJSONPath);
+if (data.private !== true) {
+  data.publishConfig = {};
+  data.publishConfig.access = 'public';
+  utils.writeJSONFile(pkgJSONPath, data);
+}
 
 // Add the extension path to packages/metapackage/tsconfig.json
 let tsconfigPath = path.join(
@@ -64,7 +70,7 @@ let tsconfig = utils.readJSONFile(tsconfigPath);
 tsconfig.compilerOptions.paths[data.name] = [
   path.join('..', packageDirName, 'src')
 ];
-fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2) + '\n');
+utils.writeJSONFile(tsconfigPath, tsconfig);
 
 // Update the core jupyterlab build dependencies.
 try {

+ 34 - 1
buildutils/src/utils.ts

@@ -3,6 +3,7 @@ import glob = require('glob');
 import fs = require('fs-extra');
 import childProcess = require('child_process');
 import sortPackageJson = require('sort-package-json');
+import coreutils = require('@phosphor/coreutils');
 
 /**
  * Get all of the lerna package paths.
@@ -50,12 +51,44 @@ export function writePackageData(pkgJsonPath: string, data: any): boolean {
 }
 
 /**
- * Read a package.json file.
+ * Read a json file.
  */
 export function readJSONFile(filePath: string): any {
   return JSON.parse(fs.readFileSync(filePath, 'utf8'));
 }
 
+/**
+ * Write a json file.
+ */
+export function writeJSONFile(filePath: string, data: any): boolean {
+  function sortObjByKey(value: any): any {
+    // https://stackoverflow.com/a/35810961
+    return typeof value === 'object'
+      ? Array.isArray(value)
+        ? value.map(sortObjByKey)
+        : Object.keys(value)
+            .sort()
+            .reduce((o: any, key) => {
+              const v = value[key];
+              o[key] = sortObjByKey(v);
+              return o;
+            }, {})
+      : value;
+  }
+  let text = JSON.stringify(data, sortObjByKey(data), 2) + '\n';
+  let orig = {};
+  try {
+    orig = readJSONFile(filePath);
+  } catch (e) {
+    // no-op
+  }
+  if (!coreutils.JSONExt.deepEqual(data, orig)) {
+    fs.writeFileSync(filePath, text, 'utf8');
+    return true;
+  }
+  return false;
+}
+
 /**
  * Run a command with terminal output.
  *

+ 3 - 0
buildutils/template/package.json

@@ -30,5 +30,8 @@
   "devDependencies": {
     "rimraf": "~2.6.2",
     "typescript": "~2.9.2"
+  },
+  "publishConfig": {
+    "access": "public"
   }
 }