Coder Social home page Coder Social logo

Comments (6)

mdonnalley avatar mdonnalley commented on May 26, 2024

@alexpchin The compiled source path is determined based on the outDir and rootDir (or rootDirs or baseUrl) - which happens here. So maybe there's something that you can tweak there to make it find the right path?

from core.

alexpchin avatar alexpchin commented on May 26, 2024

Hi @mdonnalley the reason I'm having the issue is that I'm using pnpm and I'm requiring another package without setting rootDir, just setting baseUrl. This means each package is added to the lib folder.

I could possibly look at tweaking this by looking at typescript references (I believe) but this may/may not work for us.

Is there no way to supply an alternative config just for use with ts-node as it won't be looking in the lib folder? If I change to /src/commands everything works with ts-node which obviously breaks then when using run?

from core.

mdonnalley avatar mdonnalley commented on May 26, 2024

Let me make sure I understand:

You want to use ./src/commands when running bin/dev.js and you want to use ./dist/bunk-cli/src/commands when running bin/run.js? And the reason is that tsPath can't correctly resolve ./dist/bunk-cli/src/commands back to ./src/commands?

Is there no way to supply an alternative config just for use with ts-node as it won't be looking in the lib folder?

No, I can't think of anything that would allow you to do this without some code changes.

Probably the easiest code change that could be made is to have an env var that overrides any reference to this.pjson.oclif.commands in src/config/plugin.ts

Usage would be something like:

OCLIF_COMMANDS_OVERRIDE=./src/commands bin/dev.js

Would be happy to accept a PR if you would like to make that change.

If you don't want that, I have a couple of ideas you could try on your side:

  • Precompile code prior to using bin/dev.js (I realize that this defeats the purpose of bin/dev.js)
  • Have bin/dev.js modify the package.json prior to executing and then restoring it after execution completes

Neither of those are great but you could give them a try.

from core.

alexpchin avatar alexpchin commented on May 26, 2024

@mdonnalley That's exactly the issue. A couple of ideas, could we enable passing of options to execute via loadOptions? Or specify a .oclifrc file?

this.OCLIF_CONFIG = process.env.OCLIFRC ?? 'package.json';

this.pjson = await readJson(join(root, this.OCLIF_CONFIG));

It might allow for more configuration without having to set separate options as environment variables?

Basically, the problem is that ts-node and tsc output files differently when not using typescript's rootDir in a monorepo (TypeScript determines one parent folder containing all your source files. This folder is set as [rootDir] in tsconfig.json or calculated automatically by the compiler).

So I think it would be useful to be able to set:

  • .oclifrc.dev.json for dev (ts-node)
  • .oclifrc.json for both build script and run (tsc).

Another idea, you could also possibly add the bunk-cli/src as a relativeOutDir option? I.e. when using ts-node and an relativeOutDir option is provided, remove it from the commands and hooks in package.json? To make /dist/bunk-cli/src/commands become /dist/commands? /outDir/(relativeOutDir?)/commands

from core.

mdonnalley avatar mdonnalley commented on May 26, 2024

I don't think that we're likely to add support for an rc file. As it is, we're just too dependent on the package.json (for things other than the oclif section) and I don't want to add the complexity of another configuration source.

For an unrelated reason, I added a pjson option to the LoadOptions that can be passed to execute in #945

You'd have to read the package.json, modify oclif.commands and then pass that into execute. Something like this:

const pjson = JSON.parse(await readFile('package.json', 'utf8'))
pjson.oclif.commands = './src/commands'
await execute({
  loadOptions: {
    pjson,
    root: __dirname,
  },
})

Would that work for you?

If you want, you can test it out yourself using @oclif/[email protected]

from core.

alexpchin avatar alexpchin commented on May 26, 2024

Hi @mdonnalley Thanks, that works nicely! Amazing. Just for reference this is my full dev.js

#!/usr/bin/env -S node --import ts-node/esm --no-warnings=ExperimentalWarning
// eslint-disable-next-line unicorn/prefer-top-level-await
(async () => {
  const tsConfig = await import('../tsconfig.json');
  const tsConfigPaths = await import('tsconfig-paths');

  tsConfigPaths.register({
    baseUrl: './',
    paths: tsConfig.compilerOptions.paths,
  });
  /**
   * Due to typescript building into dist in a non-flat way,
   * i.e. tsc is assuming the rootDir to be the root of the momonrepo
   * because we're requiring files from other packages, i.e. bunk-models
   * then we have to load a different location for the oclif commands
   * to work when loading with ts-node which is on-the-fly.
   */
  const { readFile } = await import('fs/promises');
  const path = await import('path');
  const pjsonPath = path.join(__dirname, '..', 'package.json');
  const data = await readFile(pjsonPath, 'utf8');
  const pjson = JSON.parse(data);
  const commandsPath = './src/commands';
  const hooksPath = './src/hooks/custom/init-config';
  pjson.oclif = {
    bin: 'bunk-cli',
    commands: commandsPath,
    hooks: {
      'init-config': hooksPath,
    },
  };
  const { execute, settings } = await import('@oclif/core');
  // settings.performanceEnabled = true;
  const root = path.join(__dirname, '..');
  await execute({
    development: true,
    loadOptions: {
      pjson,
      root,
    },
  });
})();

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.