Skip to content

Commit

Permalink
Build script to setup ESM/CJS module exports
Browse files Browse the repository at this point in the history
  • Loading branch information
gfortaine committed Jan 16, 2023
1 parent 7c16709 commit ca43fe2
Show file tree
Hide file tree
Showing 44 changed files with 1,413 additions and 693 deletions.
208 changes: 102 additions & 106 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,110 @@
'use strict';

const { prompt } = require('./');
import { prompt } from './index.js';

let interval;

(async function(){
const questions = [
{
type: 'text',
name: 'twitter',
message: `What's your twitter handle?`,
initial: `terkelg`,
format: v => `@${v}`
},
{
type: 'number',
name: 'age',
message: 'How old are you?',
validate: value => value < 18 ? `Sorry, you have to be 18` : true
},
{
type: 'password',
name: 'secret',
message: 'Tell me a secret'
},
{
type: 'confirm',
name: 'confirmed',
message: 'Can you confirm?'
},
{
type: prev => prev && 'toggle',
name: 'confirmtoggle',
message: 'Can you confirm again?',
active: 'yes',
inactive: 'no'
},
{
type: 'list',
name: 'keywords',
message: 'Enter keywords'
},
{
type: 'select',
name: 'color',
message: 'Pick a color',
choices: [
{ title: 'Red', description: 'This option has a description.', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Yellow', value: '#ffff00', disabled: true },
{ title: 'Blue', value: '#0000ff' }
]
},
{
type: 'multiselect',
name: 'multicolor',
message: 'Pick colors',
hint: false,
choices: [
{ title: 'Red', description: 'This option has a description.', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Yellow', value: '#ffff00', disabled: true },
{ title: 'Blue', value: '#0000ff' }
]
},
{
type: 'autocomplete',
name: 'actor',
message: 'Pick your favorite actor',
initial: 1,
limit: 3,
suggest: (input, choices) => choices.filter(i => i.title.toLowerCase().includes(input.toLowerCase())),
choices: [
{ title: 'Cage' },
{ title: 'Clooney', value: 'silver-fox' },
{ title: 'Gyllenhaal' },
{ title: 'Gibson' },
{ title: 'Grant', description: 'This option has a description.' },
{ title: 'Hanks' },
{ title: 'Downey Jr.' }
],
fallback: {
title: `This is the fallback. Its value is 'fallback'`,
value: 'fallback'
}
},
{
type: 'date',
name: 'birthday',
message: `What's your birthday?`,
validate: date => date > Date.now() ? `Your birth day can't be in the future` : true
},
{
type: 'number',
name: 'prompt',
message: 'This will be overridden',
onRender(color) {
this.no = (this.no || 1);
this.msg = `Enter a number (e.g. ${color.cyan(this.no)})`;
if (!interval) interval = setInterval(() => {
this.no += 1;
this.render();
}, 1000);
}
const questions = [
{
type: 'text',
name: 'twitter',
message: `What's your twitter handle?`,
initial: `terkelg`,
format: v => `@${v}`
},
{
type: 'number',
name: 'age',
message: 'How old are you?',
validate: value => value < 18 ? `Sorry, you have to be 18` : true
},
{
type: 'password',
name: 'secret',
message: 'Tell me a secret'
},
{
type: 'confirm',
name: 'confirmed',
message: 'Can you confirm?'
},
{
type: prev => prev && 'toggle',
name: 'confirmtoggle',
message: 'Can you confirm again?',
active: 'yes',
inactive: 'no'
},
{
type: 'list',
name: 'keywords',
message: 'Enter keywords'
},
{
type: 'select',
name: 'color',
message: 'Pick a color',
choices: [
{ title: 'Red', description: 'This option has a description.', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Yellow', value: '#ffff00', disabled: true },
{ title: 'Blue', value: '#0000ff' }
]
},
{
type: 'multiselect',
name: 'multicolor',
message: 'Pick colors',
hint: false,
choices: [
{ title: 'Red', description: 'This option has a description.', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Yellow', value: '#ffff00', disabled: true },
{ title: 'Blue', value: '#0000ff' }
]
},
{
type: 'autocomplete',
name: 'actor',
message: 'Pick your favorite actor',
initial: 1,
limit: 3,
suggest: (input, choices) => choices.filter(i => i.title.toLowerCase().includes(input.toLowerCase())),
choices: [
{ title: 'Cage' },
{ title: 'Clooney', value: 'silver-fox' },
{ title: 'Gyllenhaal' },
{ title: 'Gibson' },
{ title: 'Grant', description: 'This option has a description.' },
{ title: 'Hanks' },
{ title: 'Downey Jr.' }
],
fallback: {
title: `This is the fallback. Its value is 'fallback'`,
value: 'fallback'
}
},
{
type: 'date',
name: 'birthday',
message: `What's your birthday?`,
validate: date => date > Date.now() ? `Your birth day can't be in the future` : true
},
{
type: 'number',
name: 'prompt',
message: 'This will be overridden',
onRender(color) {
this.no = (this.no || 1);
this.msg = `Enter a number (e.g. ${color.cyan(this.no)})`;
if (!interval) interval = setInterval(() => {
this.no += 1;
this.render();
}, 1000);
}
];
}
];

const answers = await prompt(questions, {onCancel:cleanup, onSubmit:cleanup});
console.log(answers);
})();
const answers = await prompt(questions, {onCancel:cleanup, onSubmit:cleanup});
console.log(answers);

function cleanup() {
clearInterval(interval);
Expand Down
3 changes: 3 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const prompts = require("./dist/index.js").default;

module.exports = prompts;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module.exports = require('./lib/index.js');
import { default as prompts } from './lib/index.js';

export * from './lib/index.js';
export default prompts;
4 changes: 1 addition & 3 deletions lib/dateparts/datepart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

class DatePart {
constructor({token, date, parts, locales}) {
this.token = token;
Expand Down Expand Up @@ -30,6 +28,6 @@ class DatePart {
}
}

module.exports = DatePart;
export default DatePart;


6 changes: 2 additions & 4 deletions lib/dateparts/day.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

const pos = n => {
n = n % 10;
Expand Down Expand Up @@ -39,4 +37,4 @@ class Day extends DatePart {
}
}

module.exports = Day;
export default Day;
6 changes: 2 additions & 4 deletions lib/dateparts/hours.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Hours extends DatePart {
constructor(opts={}) {
Expand All @@ -27,4 +25,4 @@ class Hours extends DatePart {
}
}

module.exports = Hours;
export default Hours;
30 changes: 19 additions & 11 deletions lib/dateparts/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use strict';
import DatePart from './datepart.js';
import Meridiem from './meridiem.js';
import Day from './day.js';
import Hours from './hours.js';
import Milliseconds from './milliseconds.js';
import Minutes from './minutes.js';
import Month from './month.js';
import Seconds from './seconds.js';
import Year from './year.js';

module.exports = {
DatePart: require('./datepart'),
Meridiem: require('./meridiem'),
Day: require('./day'),
Hours: require('./hours'),
Milliseconds: require('./milliseconds'),
Minutes: require('./minutes'),
Month: require('./month'),
Seconds: require('./seconds'),
Year: require('./year'),
export {
DatePart,
Meridiem,
Day,
Hours,
Milliseconds,
Minutes,
Month,
Seconds,
Year,
}
6 changes: 2 additions & 4 deletions lib/dateparts/meridiem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Meridiem extends DatePart {
constructor(opts={}) {
Expand All @@ -21,4 +19,4 @@ class Meridiem extends DatePart {
}
}

module.exports = Meridiem;
export default Meridiem;
6 changes: 2 additions & 4 deletions lib/dateparts/milliseconds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Milliseconds extends DatePart {
constructor(opts={}) {
Expand All @@ -25,4 +23,4 @@ class Milliseconds extends DatePart {
}
}

module.exports = Milliseconds;
export default Milliseconds;
6 changes: 2 additions & 4 deletions lib/dateparts/minutes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Minutes extends DatePart {
constructor(opts={}) {
Expand All @@ -25,4 +23,4 @@ class Minutes extends DatePart {
}
}

module.exports = Minutes;
export default Minutes;
6 changes: 2 additions & 4 deletions lib/dateparts/month.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Month extends DatePart {
constructor(opts={}) {
Expand Down Expand Up @@ -30,4 +28,4 @@ class Month extends DatePart {
}
}

module.exports = Month;
export default Month;
6 changes: 2 additions & 4 deletions lib/dateparts/seconds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Seconds extends DatePart {
constructor(opts={}) {
Expand All @@ -25,4 +23,4 @@ class Seconds extends DatePart {
}
}

module.exports = Seconds;
export default Seconds;
6 changes: 2 additions & 4 deletions lib/dateparts/year.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const DatePart = require('./datepart');
import DatePart from './datepart.js';

class Year extends DatePart {
constructor(opts={}) {
Expand All @@ -25,4 +23,4 @@ class Year extends DatePart {
}
}

module.exports = Year;
export default Year;
Loading

0 comments on commit ca43fe2

Please sign in to comment.