Skip to content

Commit

Permalink
diy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Nov 15, 2022
1 parent 5449fd2 commit bdaf16f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 70 deletions.
28 changes: 26 additions & 2 deletions packages/adapter-auto/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { execSync } from 'child_process';
import { detect } from 'detect-package-manager';
import { pathToFileURL } from 'url';
import { resolve } from 'import-meta-resolve';
import { adapters } from './adapters.js';
import { dirname, join } from 'path';
import { existsSync } from 'fs';

/** @type {import('./index').default} */
let fn;
Expand All @@ -14,6 +15,29 @@ const commands = {
yarn: (name) => `yarn add -D ${name}`
};

function detect_lockfile() {
let dir = process.cwd();

do {
if (existsSync(join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
if (existsSync(join(dir, 'yarn.lock'))) return 'yarn';
if (existsSync(join(dir, 'package-lock.json'))) return 'npm';
} while (dir !== (dir = dirname(dir)));

return 'npm';
}

function detect_package_manager() {
const manager = detect_lockfile();

try {
execSync(`${manager} --version`);
return manager;
} catch {
return 'npm';
}
}

/** @param {string} name */
async function import_from_cwd(name) {
const cwd = pathToFileURL(process.cwd()).href;
Expand All @@ -34,7 +58,7 @@ for (const candidate of adapters) {
error.code === 'ERR_MODULE_NOT_FOUND' &&
error.message.startsWith(`Cannot find package '${candidate.module}'`)
) {
const package_manager = await detect();
const package_manager = detect_package_manager();
const command = commands[package_manager](candidate.module);

try {
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-auto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"typescript": "^4.8.4"
},
"dependencies": {
"detect-package-manager": "^2.0.1",
"import-meta-resolve": "^2.1.0"
}
}
73 changes: 6 additions & 67 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bdaf16f

Please sign in to comment.