Comments (3)
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.
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.
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
-
My problem is that the regular expression was not correct
-
It working with "\n" character
-
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) { // ... }
-
Method
addAfter
andaddBefore
should support a function as parameter
Thankyou
from preset.
Related Issues (20)
- ERR_PRESET_FILE_NOT_FOUND Could not find a preset file in "undefined". HOT 1
- Programmatic usage
- Hey programatic API is a 404 HOT 1
- replace-variable fails if git.config is missing user.name or user.email HOT 5
- Cannot use `defineAction` outside `preset.ts` file HOT 6
- Ability to import external (versioned) packages inside preset HOT 1
- Getting `contexts.at is not a function` when using with `laravel:vite` or `laravel:inertia` presets HOT 1
- @preset/cli exits with a 0 exit code on failure HOT 5
- prompt action
- Add a way to detect which package manager was used to run the preset
- Prompt Action submits data if user tries to exit
- Prompt Action doesn't handle backspace HOT 3
- Init new preset without git to use it as a local code generator HOT 3
- Global configuration file
- `filesystemMatches` helper
- Support `--path` to use a preset in a subdirectory HOT 1
- Make API global in preset projects
- Auto-retry once without SSH
- Could not find a preset file in "[object Promise]" HOT 3
- Nested Preset HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from preset.