Skip to content

Commit

Permalink
Improve accuracy of checking if package is local
Browse files Browse the repository at this point in the history
  • Loading branch information
zodern committed Jul 12, 2022
1 parent 127da4e commit 263706f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
23 changes: 18 additions & 5 deletions isopacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ let meteorParentDir = process.platform === 'win32' ?
process.env.LOCALAPPDATA :
process.env.HOME;

let catalogPath = path.join(meteorParentDir, '.meteor', 'packages');
let remoteCatalogPath = path.join(meteorParentDir, '.meteor', 'packages');

module.exports = function loadPackages(appPath) {
module.exports = function loadPackages(appPath, catalog) {
var contents;
try {
contents = fs.readFileSync(path.resolve(appPath, '.meteor/versions'), 'utf-8');
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = function loadPackages(appPath) {
if (packageVersion.package in packages)
return;

var result = findPackagePath(appPath, packageVersion.package, packageVersion.version);
var result = findPackagePath(appPath, packageVersion.package, packageVersion.version, catalog);
packages[packageVersion.package] = {
remote: result.remote,
version: packageVersion.version,
Expand Down Expand Up @@ -88,15 +88,28 @@ function readIsopack(packagePath) {
return config;
}

function findPackagePath(appPath, name, version) {
function findPackagePath(appPath, name, version, catalog) {
let checkLocal = true;

if (catalog) {
let entry = catalog.getVersion(name, version);
log('Catalog result:', name, version, `entry: ${!!entry}`, `published: ${entry && entry.published}`);
if (entry && entry.published) {
// We know the package is not local
// Checking the local isopacks could lead to incorrect results since
// Meteor doesn't remove isopacks if a package was local, then becomes remote
checkLocal = false;
}
}

// Check if local package
let localPath = path.resolve(appPath, '.meteor/local/isopacks', name.replace(':', '_'));
if (exists(localPath)) {
return { packagePath: localPath, remote: false };
}

let remotePath = path.join(
catalogPath,
remoteCatalogPath,
name.replace(':', '_'),
version
);
Expand Down
8 changes: 1 addition & 7 deletions publish-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const fs = require('fs');
const spawnSync = require('child_process').spawnSync;
const PackageSource = require('./tools-imports.js').PackageSource;

// During publishing packages, this handles:
//
Expand Down Expand Up @@ -31,13 +32,6 @@ const isPublish = process.argv.includes('publish');
const packagePath = process.cwd();
let prepared = false;

const mainModule = global.process.mainModule;
const absPath = mainModule.filename.split(path.sep).slice(0, -1).join(path.sep);
const toolsRequire = function (filePath) {
return mainModule.require(path.resolve(absPath, filePath));
};

const PackageSource = toolsRequire('isobuild/package-source');
const originalFindSources = PackageSource.prototype._findSources;

PackageSource.prototype._findSources = function (options) {
Expand Down
12 changes: 12 additions & 0 deletions tools-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path');

const mainModule = global.process.mainModule;
const absPath = mainModule.filename.split(path.sep).slice(0, -1).join(path.sep);
const toolsRequire = function (filePath) {
return mainModule.require(path.resolve(absPath, filePath));
};

module.exports = {
PackageSource: toolsRequire('isobuild/package-source'),
ProjectContext: toolsRequire('./project-context').ProjectContext
}
19 changes: 17 additions & 2 deletions types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
var loadPackages = require('./isopacks.js');
var findTypesEntry = require('./types-entry.js');
var Writer = require('./writer.js');
const ProjectContext = require('./tools-imports.js').ProjectContext;

var appPath = process.cwd();
var writer = new Writer(appPath);

var catalog;
var setupFinished = false;

const oldGetProjectLocalDirectory = ProjectContext.prototype.getProjectLocalDirectory;
// Meteor calls getProjectLocalDirectory at the beginning of every build
ProjectContext.prototype.getProjectLocalDirectory = function () {
catalog = this.projectCatalog;

return oldGetProjectLocalDirectory.apply(this, arguments);
};

Plugin.registerLinter({
// TODO: Meteor seems to be unable to start if the app has a main module
// with a different file extension than listed here??
// TODO: reduce the number of extensions to make linting faster
extensions: ['ts', 'js', 'tsx', 'jsx'],
}, () => new Linter);
}, () => new Linter());

class Linter {
processFilesForPackage(files) {
Expand All @@ -23,12 +33,17 @@ class Linter {
return;
}

if (!catalog) {
console.warn('Linter ran before we had access to package catalog');
console.warn('Please create a GitHub issue for zodern:types');
}

if (!setupFinished) {
writer.setup();
setupFinished = true;
}

var packages = loadPackages(appPath);
var packages = loadPackages(appPath, catalog);

for(var entry of Object.entries(packages)) {
var name = entry[0];
Expand Down

0 comments on commit 263706f

Please sign in to comment.