Giter Club home page Giter Club logo

Comments (3)

innocenzi avatar innocenzi commented on June 14, 2024 1

In 0.2, you'll be able to directly edit an environment file. Something like the following.

Preset.setEnv('APP_NAME', 'Laravel', '.env'); // last is optional, defaults to .env
Preset.setEnv('APP_URL', ({ env }) => `https://${env.APP_DOMAIN}`);

The edit action will have fewer features because it wasn't maintainable, and the API drastically changes anyway between 0.1 and 0.2, so I'll close this issue.

EDIT: the following API is implemented in 0.2.

Preset.env('.env') // optional, .env by default
	.set('APP_NAME', ({ APP_NAME }) => APP_NAME ?? 'Laravel')
	.set('APP_URL', ({ APP_NAME }) => `https://${APP_NAME.toLowerCase()}.test`)
	.skipIfMissing(); // by default, it's created if it's missing

from preset.

innocenzi avatar innocenzi commented on June 14, 2024

What do you mean by "it doesn't work"? Is it an issue with the \n?

This issue brings to the table the fact that it would be nice to be able to get information with Preset. In your case, you'd like to know the value of APP_URL thanks to a RegExp. This is something that could be stored in the preset context and used after, in this case with addAfter.

Of course, we could also implement what you want, but it would probably imply a lot of changes in the current not-so-pretty code.

When I've time to work again on implementing functionalities, I'll see what is the best option here and update this issue accordingly. 👍

from preset.

oanhnn avatar oanhnn commented on June 14, 2024

I setup test as below

$ mkdir -p preset-test/test
$ cd preset-test
$ npx use-preset preset
$ vi test/.env
$ tree -L 2 -I node_modules -a .
.
├── .gitignore
├── package.json
├── README.md
├── src
│   └── preset.js
├── templates
│   └── helloworld.txt
├── test
│   └── .env
└── yarn.lock

3 directories, 7 files

Content of .env file

APP_NAME=Laravel
APP_URL=http://example-laravel.test
LOG_CHANNEL=stack

Edit preset.js and run test:

$ cd test
$ npx npx use-preset ..

Case 1

const { Preset } = require('use-preset');

module.exports = Preset.make('test')
  .edit('.env')
  .replace(/^APP_URL=http:\/\/(.*)$/)
  .with((str, domain) => {
    return ['APP_URL=http://' + domain, 'APP_DOMAIN=' + domain].join('\n');
  })
  .chain();

Result

File .env don't change

Case 2

const { Preset } = require('use-preset');

module.exports = Preset.make('test')
  .edit('.env')
  .replace(/APP_URL=http:\/\/(.*)/)
  .with((str, domain) => {
    return ['APP_URL=http://' + domain, 'APP_DOMAIN=' + domain].join('\n');
  })
  .chain();

Result

APP_NAME=Laravel
APP_URL=http://undefined
APP_DOMAIN=undefined
LOG_CHANNEL=stack

Case 3

const { Preset } = require('use-preset');

module.exports = Preset.make('test')
  .edit('.env')
  .replace(/APP_URL=http:\/\/(.*)/)
  .with((str, domain) => {
    console.error(arguments)
    return ['APP_URL=http://' + str, 'APP_DOMAIN=' + domain].join('\n');
  })
  .chain();

Result

APP_NAME=Laravel
APP_URL=http://[object Object]
APP_DOMAIN=undefined
LOG_CHANNEL=stack

Console don't log error (list arguments)

Case 4

const { Preset } = require('use-preset');

module.exports = Preset.make('test')
  .edit('.env')
  .replace(/APP_URL=http:\/\/(.*)/)
  .with(function () {
	console.error(arguments)
    return 'APP_URL=http://abc.xyz\nAPP_DOMAIN=abc.xyz';
  })
  .chain();

Result

APP_NAME=Laravel
APP_URL=http://abc.xyz
APP_DOMAIN=abc.xyz
LOG_CHANNEL=stack

Console log error (list arguments)

$ npx use-preset ..
✔ Resolve
✔ Parse
❯ Apply test
  ❯ Edit file
    ✔ Validate
    ⠋ Execute
◼ Clean up temporary files
[Arguments] {
  '0': {
    generator: {
      name: 'test',
      templates: undefined,
      after: undefined,
      afterEach: undefined,
      before: undefined,
      beforeEach: undefined,
      options: [],
      actions: [Function: actions]
    },
    debug: false,
    targetDirectory: '/home/oanhnn/github/oanhnn/preset-test/test',
    task: TaskWrapper { task: [Task], errors: [], options: [Object] },
    argv: [ '..' ],
    temporary: false,
    presetName: 'test',
    presetDirectory: '/home/oanhnn/github/oanhnn/preset-test',
    presetTemplates: '/home/oanhnn/github/oanhnn/preset-test/templates',
✔ Resolve
✔ Parse
✔ Apply test
  ✔ Edit file
↓ Delete temporary directory [SKIPPED]

Reason

  1. My problem is that the regular expression was not correct

  2. It working with "\n" character

  3. Arguments list of replace function don't like reference A string or a replacement function.

    Arguments list should like:

    function (match, p1, p2, ..., offset, string, groups) {
       // ...
    }

    see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter

  4. Method addAfter and addBefore should support a function as parameter

Thankyou

from preset.

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.