Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

1.7 KB
Raw Blame
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';

const scriptsFolder = join(fileURLToPath(import.meta.url), '..', '..');
const scriptsSrcFolder = join(scriptsFolder, 'src');
const projectRoot = join(scriptsFolder, '..', '..');

const [node, _self, file, ...options] = process.argv;

if (!file) {
  console.error(`Please provide a file to run, e.g. 'run src/index.{js/ts}'`);
  process.exit(1);
}

const fileLocationCandidates = new Set([
  process.cwd(),
  scriptsSrcFolder,
  projectRoot,
]);
const lookups = [];

/**
 * @type {string | undefined}
 */
let scriptLocation;
for (const location of fileLocationCandidates) {
  if (scriptLocation) {
    break;
  }

  const fileCandidates = [file, `${file}.js`, `${file}.ts`];
  for (const candidate of fileCandidates) {
    const candidateLocation = join(location, candidate);
    if (existsSync(candidateLocation)) {
      scriptLocation = candidateLocation;
      break;
    }
    lookups.push(candidateLocation);
  }
}

if (!scriptLocation) {
  console.error(
    `File ${file} not found, please make sure the first parameter passed to 'run' script is a valid js or ts file.`
  );
  console.error(`Searched locations: `);
  lookups.forEach(location => {
    console.error(`  - ${location}`);
  });
  process.exit(1);
}

const nodeOptions = [];

if (
  scriptLocation.endsWith('.ts') ||
  scriptLocation.startsWith(scriptsFolder)
) {
  nodeOptions.unshift(`--import=@oxc-node/core/register`);
}

spawn(node, [...nodeOptions, scriptLocation, ...options], {
  stdio: 'inherit',
}).on('exit', code => {
  process.exit(code);
});