{"id":1505,"date":"2024-04-01T16:37:50","date_gmt":"2024-04-01T22:37:50","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=1505"},"modified":"2024-04-01T16:37:51","modified_gmt":"2024-04-01T22:37:51","slug":"a-quick-command-to-see-the-available-scripts","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/a-quick-command-to-see-the-available-scripts\/","title":{"rendered":"A Quick Command To See the Available Scripts"},"content":{"rendered":"\n<p>Have you ever been like, <em>ughkgh<\/em>, is it <code>npm run dev<\/code> or <code>npm run start<\/code>? Is it <code>yarn testwatch<\/code> or <code>yarn test:watch<\/code>? If you do it <em>every<\/em> time you open a project, I&#8217;d <a href=\"https:\/\/frontendmasters.com\/blog\/vs-code-auto-run-commands\/\">suggest setting up tasks<\/a>. But I mean more for one-off or less frequently used scripts. I&#8217;m forever forgetting the exact <code>scripts<\/code> from the <code>package.json<\/code> that are set up in the project (or subproject of a monorepo) that I&#8217;m in. <\/p>\n\n\n\n<p>I was pair programming with a co-worker earlier, and I noticed they had this little moment where they couldn&#8217;t remember the name of a script, and they just typed <code>scripts<\/code> and it gave them nice output of all the scripts from the <code>package.json<\/code> file that could be run. I was like <em>whaaaat. <\/em>Is <code>scripts<\/code> just some command I never heard about? No, no. It was just a custom alias they had set up. <\/p>\n\n\n\n<p>It&#8217;s just this command, they told me:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\">cat package.json | jq --color-output <span class=\"hljs-string\">'.scripts'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notably, that requires usage of <a href=\"https:\/\/jqlang.github.io\/jq\/\">the <code>jq<\/code> package<\/a>, so you&#8217;d need to install that (globally) to use this command. But we use that as part of our dev environment here at work so we both had it anyway. As a front-end guy, I&#8217;m understanding <code>jq<\/code> sorta like <code>querySelectorAll<\/code> for JSON. Since <code>package.json<\/code> is obviously JSON, it can grab out parts of it easily. So that command above reads like:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Grab the contents of the <code>package.json<\/code> file in the current directory<\/li>\n\n\n\n<li>Send the contents of it to the next command<\/li>\n\n\n\n<li>Invoke <code>jq<\/code><\/li>\n\n\n\n<li>Tell it to colorize the output<\/li>\n\n\n\n<li>Output only the content of the <code>\"scripts\"<\/code> part of the JSON.<\/li>\n<\/ol>\n\n\n\n<p>Then to make that command easier to reach for, he set up (and I followed suit) with an <code>alias<\/code>. I use ZSH, so updated my <code>.zshrc<\/code> file (I always open this with <code>code ~\/.zshrc<\/code>), but it should work in Bash and the <code>.bashrc<\/code> file as well. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Bash\" data-shcb-language-slug=\"bash\"><span><code class=\"hljs language-bash\"><span class=\"hljs-built_in\">alias<\/span> scripts=<span class=\"hljs-string\">\"cat package.json | jq --color-output '.scripts'\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Bash<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">bash<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So yes, I <em>could<\/em> just manually open the <code>package.json<\/code> file and find the <code>\"scripts\"<\/code> area and have a look. But now I can just type <code>scripts<\/code> into the command line and it will output a perfectly nice bit of scoped JSON to peak at: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"406\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/04\/scripts.png?resize=1024%2C406&#038;ssl=1\" alt=\"\" class=\"wp-image-1508\" style=\"width:501px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/04\/scripts.png?resize=1024%2C406&amp;ssl=1 1024w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/04\/scripts.png?resize=300%2C119&amp;ssl=1 300w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/04\/scripts.png?resize=768%2C304&amp;ssl=1 768w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/04\/scripts.png?w=1120&amp;ssl=1 1120w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever been like, ughkgh, is it npm run dev or npm run start? Is it yarn testwatch or yarn test:watch? If you do it every time you open a project, I&#8217;d suggest setting up tasks. But I mean more for one-off or less frequently used scripts. I&#8217;m forever forgetting the exact scripts from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1507,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[142],"class_list":["post-1505","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-cli"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/04\/scripts-thumb.jpg?fit=1000%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=1505"}],"version-history":[{"count":3,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1505\/revisions"}],"predecessor-version":[{"id":1510,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1505\/revisions\/1510"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/1507"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=1505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=1505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=1505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}